Linux show which shell

How to test what shell I am using in a terminal?

I am not so sure that «shell» has a well defined meaning. For example, you might run xterm -e /bin/cat but I am not happy calling /bin/cat a shell.

13 Answers 13

Several ways, from most to least reliable (and most-to-least «heavy»):

  1. ps -p$$ -ocmd= . (On Solaris, this may need to be ps -p$$ -ofname= and on macOS and on BSD should be ps -p$$ -ocommand= .)
  2. Check for $BASH_VERSION , $ZSH_VERSION , and other shell-specific variables.
  3. Check $SHELL ; this is a last resort, as it specifies your default shell and not necessarily the current shell.

I don’t like $0 because it’s more complicated: (1) it may be just the basename, (2) it may have ‘-‘ on the front to designate it as a login shell.

@geekosaur: maybe so, but $0 still seems more useful than $SHELL : wouldn’t you agree? You could always pipe it through sed to remove the ‘-‘.

If you’re running tcsh , $tcsh and $version will be set. These are shell variables, not environment variables. If you’re running a non-tcsh version of csh , I don’t think there are any distinctive variables. And of course the syntax used to check variables differs between csh/tcsh on the one hand, and sh/ksh/bash/zsh on the other.

I’ve found that the following works in the four shells I have installed on my system (bash, dash, zsh, csh):

The following works on zsh, bash, and dash, but not on csh:

I think that @jiliagre’s answer is probably would I would use today. On fish %self can be used in place of $$

As the question asks for the shell used and does not talk about the potential arguments passed to it, here is a way that avoid showing them:

There are two really simple ways:

  • -h or finishing all options with = for not showing any header.
  • -o comm for showing only the process basename ( bash instead of /bin/bash ).
  • -p list only process whith PID form list suplied.

This /proc/PID/exe links to the file being executed, which in this case would point to /bin/bash, /bin/ksh, etc. For getting only the name of the shell you can just use

basename $(readlink /proc/$$/exe) 

The usage of /proc is really useful via the /proc/self , which links to the PID of the current command.

basename $(readlink /proc/$$/exe) is Korn/POSIX shell syntax. Won’t work in csh/tcsh/rc/es/akanga/fish. $$ won’t work in rc / es / akanga / fish .

basename $(readlink /proc/$$/exe) is the only command here that could work for me in a docker image with no ps installed.

A note about some lighter implementations (Android phones, busybox, etc.): ps doesn’t always have support for the -p switch, but you can accomplish the search with a command like ps | grep «^$$ » . (This grep regex will uniquely identify the PID, so there will not be any false positives.)

ps | grep $$ can still give false positives if, for example, your current process is 1234 and there’s a process 12345 .

Читайте также:  Информация о системе linux astra

A mix of all the other answers, compatible with Mac (comm), Solaris (fname) and Linux (cmd):

ps -p$$ -o cmd="",comm="",fname="" 2>/dev/null | sed 's/^-//' | grep -oE '\w+' | head -n1 

this gives me my current directory name; also, under csh and tcsh it gives me Ambiguous output redirect.

This is also what Anaconda uses (version 2022.05) in it’s [env]/bin/activate script to detect shell type. A comment in that file explicitly links to this answer.

If you have it saved in your environment variables you can use the following:

For accuracy in many different shells you should use

What is that number?

It’s the process id of the current shell.

$ expands to the same value as the current shell.

$$ process ID of the parent in a sub shell

If you only want then name of the shell you could use

ps -p $$ | awk '1)print>' | awk '$0=$NF' | tr -d - 

In a nutshell we are taking the output of the process sub shell and piping it to some formatting tools awk, sed and tr all work for this, removing the first 3 columns, the first line of output, and then the — gives just the name of the shell. Consider putting that into a function for ease later.

That will most likely return the pathname of the shell executable of your login shell. It is not certain that the login shell is what you are currently running though.

I set $MYSHELL for future tests in my shell-agnostic ~/.aliases :

unset MYSHELL if [ -n "$ZSH_VERSION" ] && type zstyle >/dev/null 2>&1; then # zsh MYSHELL=`command -v zsh` elif [ -x "$BASH_VERSION" ] && type caller >/dev/null 2>&1; then # bash MYSHELL=`command -v bash` elif [ -x "$shell" ] && which setenv |grep -l builtin >/dev/null; then # tcsh echo "DANGER: this script is likely not compatible with C shells!" sleep 5 setenv MYSHELL "$shell" fi # verify if [ ! -x "$MYSHELL" ]; then MYSHELL=`command -v "$(ps $$ |awk 'NR == 2 < print $NF >')"` [ -x "$MYSHELL" ] || MYSHELL="$" # default if verify fails fi 

The tcsh section is likely unwise to roll into a POSIX-style script since it’s so radically different (thus the warning and five second pause). (For one, csh -style shells can’t do 2>/dev/null or >&2 , as noted in the famous Csh Programming Considered Harmful rant.)

Источник

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 to Find Which Shell You Are Using on Linux

Here are four simple commands to find out which shell are you using in Linux.

Which Shell am I using in Linux? Is that even a question? Of course, it is. There are several shell available for Linux systems. Some of the most popular ones are:

You may wonder this in a few situations.

For example, if you log into a Linux system not known to you, you may wonder which shell is being used by default. If you frequently change shell in Linux, you might wonder which shell you are using at the moment.

Let me show you various Linux commands to find out which shell you are using.

Find out which shell you are using in Linux

Now there is no command that will give you this output with 100% accuracy for all the shells. But these commands should be accurate for most of the shells.

Method 1

You can use the special shell parameter $$. “$$” indicates the process id of the current instance of the shell you are running. This is a read-only parameter and cannot be modified.

If you try to see the process information, it should show you the process name i.e. the shell here.

The output should be like this:

PID TTY TIME CMD 15012 pts/0 00:00:00 zsh

Method 2

You can also use $0. $0 can be the name of the shell or the name of shell script. When it is used inside a shell script, it denotes the name of the script.

But if you use it in a shell without filename, it will show the name of the shell.

The output will simply have the name of the shell:

Method 3

You can also use the pstree command. pstree means process tree and it shows all the running processes as a tree.

If you provide it with no argument, it will show all the processes from init or systemd.

However, if you give it a process id, it will show all that processes as the root of the tree. In other words, it will show all the processes initiated by that process.

You can use the same $$ bash parameter we saw in method 1.

Method 4

The last method is using the files in proc directory. If you read the article about checking CPU info in Linux, you probably already know that this directory contains the runtime system information about your Linux system.

You can use the following command to get the shell you are using:

The output should give you the name of the shell.

Bonus Tip: Know the version of the shell

So you learned how to know which shell you are using. What about the version of the shell you are using? You can get the version of the shell simply by adding –version after the name of your running shell.

For example, if you are running zsh, you can use this:

The output will give you the version information:

zsh 5.4.2 (x86_64-ubuntu-linux-gnu)

I hope this quick tutorial helped you in finding out which shell you are running. Don’t forget to check out another simple tip on changing shell in Linux.

By the way, do you use some other way to check which shell you are using? Why not share it with us in the comment section?

Источник

4 Ways to Check Which Shell You are Using on Linux

check user shell linux

Out of the box, Linux provides a wide variety of shells. There is bash (Bourne Again shell) shell which ships by default in many Linux distributions. We also have sh (Bourne Shell), tcsh (TC shell), csh (C shell), Zsh (Z shell) and ksh (Korn Shell).

Curious to know which shell you are using on your Linux system? In this guide, we explore different ways that you can use to check which shell you are currently using in Linux.

1. Using echo command

The Linux echo command is a built-in command that is used to print the output of a string which is passed as an argument. Additionally, you can use the echo command to check the shell that you are running commands in. To accomplish this, execute:

Output of echo $SHELL

The output shows that I am using the bash shell. Additionally, you can simply run the command:

Output of echo

To get the PID of the shell that you are currently in, run:

Output of echo $

2. Using ps command

Commonly used for listing running processes, the ps command in its basic format sheds light on the shell that you are using. Simply execute the command:

output of ps command

From the first line of the output, we can clearly see the PID of the shell and the last column prints out the type of shell, in this case — bash.

Alternatively, you can run the command:

Output of ps -p $

You can also use ps -p $$ -o args= which output just the shell name.

3. By viewing /etc/passwd file

The grep command can be used to probe the /etc/passwd file that contains attributes of the users such as username, user ID and group ID.

To display the shell used, invoke the command:

Using /etc/passwd file to show shell

At the very last segment, we get to see the bash used, in this case /bin/bash This also gives you a glimpse of which shell is opened first when you first log in to your system.

4. Using lsof command

Ordinarily, the lsof command, short for list of open files, is used to provide a list of open files on your system. However, when used with the -p $$ flag, it gives a pointer to the shell you are in when you look at the first column of the output.

For example, we can clearly see that we are on the bash shell.

Output of lsof -p $

How to check the valid login shells

We have seen various ways that you can employ to check the shell that you are currently in. If you want to know the valid shells on your system, check the /etc/shells file, This file will provide you with the full pathnames of valid login shells in your system. Using the cat command, view the file as shown:

Valid login shells

Conclusion

In this guide, we have shared simple but nifty ways that you can use to know which shell you are working on. This is important when writing scripts so that you can know how to start off writing the shebang header. We do hope that this tutorial was beneficial. Send us a shout and don’t forget to share this guide on your social platforms.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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