Linux find what shell

How can I know which shell I am using?

I am writing a shell script. The tutorial that I am reading have the first line like this : #!/usr/bin/env bash/ but it isn’t working for me. ( error : no such directory ) How can I find out which bash I am using and where is it located? Appreciate for any advice and help. Thanks a lot. It works now. solution is #!/usr/bin/env bash Another problem: Why it just can’t read the word ‘restart’ my code in the start.sh:

#!/usr/bin/env bash/ RESTART="apachectl restart" $RESTART 
Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file] [-C "directive"] [-c "directive"] [-k start|restart|graceful|graceful-stop|sto p] [-v] [-V] [-h] [-l] [-L] [-t] [-S] Options: -D name : define a name for use in directives -d directory : specify an alternate initial ServerRoot -f file : specify an alternate ServerConfigFile -C "directive" : process directive before reading config files -c "directive" : process directive after reading config files -e level : show startup errors of level (see LogLevel) -E file : log startup errors to file -v : show version number -V : show compile settings -h : list available command line options (this page) -l : list compiled in modules -L : list available configuration directives -t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings) -S : a synonym for -t -D DUMP_VHOSTS -t -D DUMP_MODULES : show all loaded modules -M : a synonym for -t -D DUMP_MODULES -t : run syntax check for config files 

why is it like that? it seems that it can read the word restart. Thank you all! I have fixed it now. solution: edit the file in unix (vim/nano and whatever but not in windows)

Источник

How do I check which shell I am using?

Please note that terminal is interface to the shell (which at one point used to be actually physical interface), and shell is not terminal — it is a command interpreter. See also askubuntu.com/a/640105/295286

9 Answers 9

You can type the following command in your terminal to see which shell you are using:

The result will look something similar to the below if you are using the bash (Bourne Again Shell) terminal:

@Mi_Onim $0 is the name of the running process. If you use it inside of a shell then it will return the name of the shell. If you use it inside of a script, it will be the name of the script.

@Anwar makes a good point, after you get the shell name from echo $0 , ls -l `which ` to see if it is symlinked to another shell binary.

To find the shell you have on the default environment you can check the value of the SHELL environment variable:

To find the current shell instance, look for the process (shell) having the PID of the current shell instance.

To find the PID of the current instance of shell:

Now to find the process having the PID:

$SHELL is the default shell for the system (or user), which is usually (but not necessarily) the shell that is actually being used at any given moment.

Читайте также:  Linux rpath or rpath

echo $SHELL gave me /bin/csh and ps -p $$ gave me 22673 pts/1 00:00:00 bash . Kingmilo explained (above) why they are not the same.

To grab ONLY the shell name for the current shell in a way portable between gnu & bsd ps versions, this works well: ps -cp «$$» -o command=»»

$SHELL gives you the default shell. $0 gives you the current shell.

For example: I have bash as my default shell, which I use for my Terminal App. But for my iTerm2 app, I use the command as the window opens: /bin/ksh .

So my $0 gives me /bin/ksh on iTerm2. $SHELL gives me /bin/bash on iTerm2. $0 , $SHELL gives me /bin/bash on Terminal

For me, I have zsh installed and it’s the current default, to change to bash, all I have to do type bash in the terminal. To switch back to zsh, type zsh

You don’t want to keep going back and forth that way because you stack up shell within shell within shell, and a fresh context within each. In general it’s best to type ctrl-d or exit to return to the previous shell.

Best answer, should be chosen as answer. Agree to yousuf Azad on changing shell, but also agree to stacking issue indicated by Phill Apley. My default is also zsh , but to switch I type bash , and to return back I can simply do exit or ctrl + D

The other answers tend to be using shell specific features, but we are trying to discover which shell we are using, so they assume the answer to the problem. For example none of the answers will work on fish.

sh -c 'ps -p $$ -o ppid=' | xargs ps -o comm= -p 

Instead use the $$ syntax in an invocation of sh, but then we are looking for the PPID not the PID. Use the PPID to find the cmd.

sh -c 'ps -p $$ -o ppid=' | xargs -I'<>' readlink -f '/proc/<>/exe' 

Thanks for improvement @muru

You ca use ppid= / cmd= to omit the headers (and so the tail -1 s), and consider looking at /proc/. /exe to see what file is being run (since the cmd output can be manipulated by whatever ran the shell).

Apparently this does not work in busybox, due to non posix compliance: ref: github.com/broadinstitute/cromwell/pull/…

this solution solves the problem of assuming the shell but introduces the problem of assuming the OS. The BSD version of readlink shipped with macOS (and presumably other BSD Distros) uses -f to specify «format» which is a printf style string that must start and end with % and has a variety of templating options.

Great answer, @EvanBenn, thanks! Here is a tiny modification to allow it to work with shells like BusyBox , where the -p arg to ps isn’t supported, and where there aren’t a whole lot of other utilities like cut to help. Code: set — $(ps -o pid,ppid | grep -E «^ *$$»); readlink -f «/proc/$<2>/exe» . I’m using your method to determine the shells used on a bunch of «free online Linux shell» websites, and it’s great!

Читайте также:  Linux какие сигналы можно перехватывать

The original post asked three questions. The answers given do cover the first question, «When I open a terminal window, which shell is opened by default?» They also answer a question which was NOT asked, namely «How can I find out which shell is currently running in the terminal?» However, as far as I can see nobody has answered either the second or third questions originally asked, namely «How do I check how many shells are installed?» and «How do I change the shell used from my account?»

    To answer «How do I check how many shells are installed?» the following command will list all the available shells:

# /etc/shells: valid login shells /bin/sh /bin/dash /bin/bash /bin/rbash 

To know which is the default shell for your user, you can run:

For example if you’re using Bash you should get the following output:

If you didn’t change any configuration it should be Bash since Bash it’s the default shell on Ubuntu.

@kol there was before edit. Please also note $SHELL is the default shell for the system (or user), which is usually (but not necessarily) the shell that is actually being used at any given moment.

@kingmilo Reading the answer again i think i understand what is your concern, probably i didn’t express myself well. I clarified the meaning of «currently» in my answer

@frederickjh fish is a bit of an odd one out here. The $$ variable is actually defined by POSIX and will work on the vast majority of shells. Fish has decided not to follow the standard here so I think it’s fair to ignore it. I can confirm that $$ works as expected in sh, dash, bash, zsh, ksh, ash, tcsh and csh. In fact, off the top of my head I can’t think of any other shell except fish where it doesn’t work.

You may not want to know the current shell’s name (e.g. -bash , bash , zsh , etc., from echo $0 ), nor default shell’s executable path (from echo $SHELL ), but rather the current shell’s executable path (especially useful e.g. if you have more than one version of Bash installed).

To do this you can use lsof -p «$$» or with some extra coding to extract just the required info:

lsof -p "$$" | grep -m 1 txt | xargs -n 1 | tail -n 1 

Example output for Bash installed via Homebrew:

/usr/local/Cellar/bash/5.1.8/bin/bash 

The above is different from echo $SHELL , both because the above is for the shell which is currently running rather than the user’s default shell, and also because the above gives the executable after any symlinks have been expanded. E.g. for the same Bash install as above, echo $SHELL gives /usr/local/bin/bash .

EDIT: If you need to allow for possible space characters in the shell’s path, use lsof -p «$$» | grep -m 1 txt | xargs -n 1 | tail -n +9 | xargs instead.

To address your third question, «How do I change the shell used from my account?», the answer is to use chsh .

You will use a program called chsh. There is a interactive method and non-interactive method. Type the following into your terminal:

INTERACTIVE METHOD

This results in a brief dialog in which the user is prompted first for their password and then for the full path of the desired new shell.

Читайте также:  Linux delete file with find

Caution should be exercised when changing one’s default shell because it is possible to make an error that only the root user (i.e., system administrator) can repair (although it should be easy for a skilled user to repair it on a home system). In particular, it is important to first test the shell temporarily in the current session and then to make certain that a valid shell name is being entered when making the permanent change.

NON-INTERACTIVE METHOD

I will use csh as again an example.

The -s sets it for you without having to go into the editor to do it.

Once this is executed successfully, then echo $SHELL will still say that you are in the same shell as before. However, you need to log out and back in for the change to take effect. Now do echo $SHELL . You should see it shows the new shell.

Источник

How do I tell what type my shell is

How can I tell what type my shell is? ie, whether it’s traditional sh, bash, ksh, csh, zsh etc. Note that checking $SHELL or $0 won’t work because $SHELL isn’t set by all shells, so if you start in one shell and then start a different one you may still have the old $SHELL . $0 only tells you where the shell binary is, but doesn’t tell you whether /bin/sh is a real Bourne shell or bash. I presume that the answer will be «try some features and see what breaks», so if anyone can point me at a script that does that, that’d be great.

7 Answers 7

This is what I use in my .profile :

# .profile is sourced at login by sh and ksh. The zsh sources .zshrc and # bash sources .bashrc. To get the same behaviour from zsh and bash as well # I suggest "cd; ln -s .profile .zshrc; ln -s .profile .bashrc". # Determine what (Bourne compatible) shell we are running under. Put the result # in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type. if test -n "$ZSH_VERSION"; then PROFILE_SHELL=zsh elif test -n "$BASH_VERSION"; then PROFILE_SHELL=bash elif test -n "$KSH_VERSION"; then PROFILE_SHELL=ksh elif test -n "$FCEDIT"; then PROFILE_SHELL=ksh elif test -n "$PS3"; then PROFILE_SHELL=unknown else PROFILE_SHELL=sh fi 

It does not make fine distinctions between ksh88, ksh95, pdksh or mksh etc., but in more than ten years it has proven to work for me as designed on all the systems I were at home on (BSD, SunOS, Solaris, Linux, Unicos, HP-UX, AIX, IRIX, MicroStation, Cygwin.)

I don’t see the need to check for csh in .profile , as csh sources other files at startup. Any script you write does not need to check for csh vs Bourne-heritage because you explicitly name the interpreter in the shebang line.

Источник

Оцените статью
Adblock
detector