The shell executes a program in response to its prompt. When you give a command, the shell searches for the program, and then executes it. For example, when you give the command ls, the shell searches for the utility/program named ls, and then runs it in the shell. The arguments and the options that you provide with the utilities can impact the result that you get. The shell is also known as a CLI, or command line interface.
Changing default shell[edit | edit source]
Most modern distributions will come with BASH (Bourne Again SHell) pre-installed and configured as a default shell.
The command (actually an executable binary, an ELF) that is responsible for changing shells in Linux is
chsh (change shell).
We can first check which shells are already installed and configured on our machine by using the
chsh -l command, which will output a result similar to this:
[user@localhost ~]$ chsh -l /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /usr/bin/fish
In some Linux distributions,
chsh -l is invalid. In this case, the list of all available shells can be found at /etc/shells file. You can show the file contents with
[user@localhost ~]$ cat /etc/shells # /etc/shells: valid login shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /usr/bin/fish
Now we can choose our new default shell, e.g.
fish, and configure it by using
[user@localhost ~]$ chsh -s /usr/bin/fish Changing shell for user. Password: Shell changed.
Now all that is left to do is preform a logoff-logon cycle, and enjoy our new default shell.
If you wish to change the default shell for a different user, and you have administrative privileges on the machine, you'll be able to accomplish this by using
root. So assuming we want to change
user_2's default shell to fish, we will use the same command as before, but with the addition of the other user's username,
chsh -s /usr/bin/fish user_2.
In order to check what the current default shell is, we can view the
$SHELL environment variable, which points to the path to our default shell, so after our change, we would expect to get a result similar to this,
~ î° echo $SHELL /usr/bin/fish
chsh options:[edit | edit source]
Sets shell as the login shell.
Print the list of shells listed in /etc/shells and exit.
Print a usage message and exit.
Print version information and exit.
Basic Shell Utilities[edit | edit source]
Customizing the Shell prompt[edit | edit source]
Default command prompt can be changed to look different and short. In case the current directory is long default command prompt becomes too large. Using
PS1 becomes useful in these cases. A short and customized command pretty and elegant. In the table below
PS1 has been used with a number of arguments to show different forms of shell prompts. Default command prompt looks something like this:
user@host ~ $ in my case it looks like this:
bruce@gotham ~ $. It can changed as per the table below:
|PS1='\w $ '|
|PS1='\h $ '|
|PS1='\u $ '|
|PS1='\t $ '|
|PS1='@ $ '|
|PS1='! $ '|
|PS1='dude $ '|
Some basic shell commands[edit | edit source]
||will take cursor to the start of the line|
||will take cursor to the end of the line|
||will delete the character after/at the cursor|
||will clear the screen/terminal|
||will clear everything between prompt and the cursor|
||will undo the last thing typed on the command line|
||will interrupt/stop the job/process running in the foreground|
||reverse search in history|
||stores last 500 commands/events used on the shell|
||will show the command history|
|grep <key-word>||will show all the commands in history having keyword <key-word> (useful in cases when you remember part of the command used in the past)|
Create Your Own Command Alias[edit | edit source]
If you are tired of using long commands in bash you can create your own command alias.
The best way to do this is to modify (or create if it does not exist) a file called .bash_aliases in your home folder. The general syntax is:
actual_command is the command you are renaming and
command_alias is the new name you have given it. For example
alias install='sudo apt-get -y install'
maps the new command alias
install to the actual command
sudo apt-get -y install. This means that when you use install in a terminal this is interpreted by bash as sudo apt-get -y install.
Locate a file on your system[edit | edit source]
Using bash you can easily locate a file with the
locate command. For example say you are looking for the file mykey.pem:
Sometimes files have strange names for example you might have a file like
random7897_mykey_0fidw.pem. Let's say you're looking for this file but you only remember the mykey and pem parts. You could combine the
locate command with
grep using a pipe like this:
locate pem | grep mykey
Which would bring up all results which contain both of these pieces.
Note that not all systems have the
locate utility installed, and many that do have not enabled it.
locate is fast and efficient because it periodically scans your system and caches the names and locations for every file on it, but if that data collection is not enabled then it cannot tell you anything. You can use
updatedb to manually initiate the filesystem scan in order to update the cached info about files on your filesystem.
Should you not have a working
locate, you can fall back on the
find / -name mykey.pem -print
is roughly equivalent to
locate mykey.pem but has to scan your filesystem(s) each time you run it for the file in question, rather than using cached data. This is obviously slower and less efficient, but more real-time. The
find utility can do much more than find files, but a full description of its capabilities is beyond the scope of this example.