Linux know 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)

Источник

What shell am I in?

Is there a command to identify the name/type of current shell, the path to the shell binary, and the version of the shell? I don’t need all of that, but the more I can get, the better. I want something that has the same feel of uname , pwd , whoami . Just a plain utility with a simple output. (which so far hasn’t showed up :/ )

re ps

Why -bash instead of the full path as it would be with everything else? What’s the deal with the dash there?

I’m starting to think the ‘test for features not for browsers’ thinking we use in javascript for the web may apply here.

I always like to counter a question with a question of my own: why do you need to know this—how do you intend to use that information and to what purpose?

8 Answers 8

The command or path to the currently running shell is stored in the environment variable $0 . To see its value, use:

Читайте также:  Linux touch for ubuntu

This outputs either your currently running shell or the path to your currently running shell, depending on how it was invoked. Some processing might be required:

prompt:~$ echo $0 /bin/bash prompt:~$ sh sh-4.0$ echo $0 sh sh-4.0$ exit exit prompt:~$ /bin/sh sh-4.0$ echo $0 /bin/sh sh-4.0$ 

The $SHELL environment variable contains the user’s preferred shell, not necessarily the currently running shell.

So, $0 actually gives you the name of the currently running script. So, inside a script it’ll give you the name of the script, not the name of the shell that’s interpreting the script.

Well, technically, $0 gives you whatever the program executing your program decided to pass to the exec-family of syscalls. Try it on a login shell, for example, and you may see -bash — with the — in front.

If you don’t specify a program in the shebang line, I believe /bin/sh will be used. Unfortunately, I don’t believe there is a good portable way to determine what that shell is.

If you’re on e.g., Linux, you can find out the executable path through /proc :

and getting the executable name is easy through ps $$ .

But that won’t help you with the type of shell (except via a lookup table of known shells) nor with the version (AFAICT, there isn’t even a way to get the version from dash)

Hmm, you could use lsof -p $$ and look for the first txt entry, that seemed to do it on my Mac. But wow is that fragile.

So much for my quest for a simple solution. But it’s been fun so far. Using lsof was way far out there.

If no interpreter is specified, the currently running shell will be used rather than /bin/sh. (This is why people that use csh as their login shell and write scripts with no shabang belong in the 4th circle of hell.)

Try ($$ is shell variable set to process id of the shell):

or try this (/proc/self is aloso process id of the shell):

As regards to «-bash» — dash means it’s login shell. Type bash again and now you’ll see that the shell is just «bash» (without dash)

Yep, I agree these are more precise commands. This probably one of the «lazy» habits to use the same command for all tasks 🙂

@kch: Actually, -o comm according to POSIX. They had to abbreviate it for some reason. But it may have the same problems as $0 .

I think ‘finger’ is the right one you are looking for. Try this command:

Rather than trying to determine the shell currently being used, it is typically more appropriate to simply re-exec as the desired shell. This may be nothing more than an historical workaround of the fact that there is no reliable, portable way to determine the shell you are currently using. The best thing to do is to write your script to work in as many shells as possible so that it’s not an issue. (eg, portability matters, no matter how many people want to claim that «bash is everywhere»)

Читайте также:  Переустановка аудио драйвера linux

You can use the following in Arch Linux.

Источник

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.

Источник

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.

Читайте также:  Linux все команды path

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