The command line shell
When we are working at the command line, or the CLI, (command line interface), we are in a shell. In Linux this may be BASH, the Bourne Again Shell are other shells such as KSH, the Korn Shell or the Bourne Shell, SH. They all so the same job but with differing bells and whistles. Your default shell that you will be given is listed as the final field against your name in the /etc/passwd file. Users can change their own default shell by using the chsh command, (change shell). If you are uncertain what shell you can change to, then run the command:
chsh -l
This will list available shells. You can only change to shells listed in the file /etc/shells which administrators manage. The output from my SUSE 11 Server is shown in the following screenshot.
I can verify my current shell with the ps command or by looking at the content of the $SHELL variable:
echo $SHELL
If I then decide that I would like to change my default shell I can use the chsh command accordingly:
chsh -s /bin/ksh
This would change my default shell to the KORN shell but not my current shell. I would need to logout and login again to collect my new default shell. As a simple test though you can use the substitute user command:
su -l geeko
If geeko is your username.
The video is a great way to help your understanding….
Entering and Leaving the Shell
Here we look at how we can access the shell or command line. If we are using a GUI environment we may be using the gnome-terminal or xterm programs. Right-clicking on the desktop in gnome can give you access to the gnome-terminal as can using the Alt + F2 keytrokes to bring up the run dialog. Should we want to access the actual terminal screens on the Linux machine we can use the Ctrl + Alt + F1 , ( or F1 through to F6); to return to the GUI we can use Alt + F7. To leave the shell exit and logout will both work. So long as the shell option is not set to ignoreeof then we an also use the Ctrl + D keystrokes. Another, more obscure way to exit a shell is the use of the exec command. This is often set in a login scripts that may deliver just one command, perhaps a menu system. Once the menu or command is finished then the shell will also exit. We can test this from within a shell:
exec ls
This will have the effect of running the command ls and then immediately exiting the shell.
Variables
Now we turn our attention to variables in our bash shell. We saw before from the previous video on chsl that we can read the contents of variables and when we do so we prefix them with the $ symbol. But there is a little more than that, as you might imagine. Variables in Linux, as with any other Operating Systems help control and customize the user environment, the option cd – allows you to change to your previous directory; Linux knows your previous directory by reading the variable $OLDPWD . These system variables are often in upper case as variables, like files, are case-sensitive; the recommendation is for you own variables to use low case so not to interfere with system variables. To create and populate a variable:
fruit=apple
The creates the variable fruit and populates it with the value of apple. Variables are local to the shell they are created in, they are said to have local scope. The make them available to sub-shell, shells launched form the current shell, then they need to be exported, making the variable global. To export an existiing variable wwe can use the export command:
export fruit
To create the variable as a global variable from the start
export fruit=apple
To read in a variable we can make use of the command echo.
echo $fruit
In most situation we will need the $ to proceed the variable when reading in the value. The clear, or delete a variable we can use the command unset :
unset fruit
The BASH shell will usually allow us tab completion on variable names when using command like echo, so we could type the $ character and then double tab to see a listing of all variables. Likewise we could use the command env. If we always want our variables to be exported we an set a shell option “allexport“. The variable $SHELLOPTS can show us the current options or using the command set: set +o To turn on the option allexport use the command;
set -o allexport
and
set +o allexport
to turn the option off again.
Aliases
Although not directly a topic for this objective it certainly fits well into this chapter next to variables as aliases too, customize the users shell experience.
Alias is a simple command in itself with just the one option -p, so simple it is not required: alias -p will print out your current aliases, but alias on its own will do this too .
Firstly we must understand what an alias is and why we need them. If we type ls at the command line in most Linux distributions we will not run the command ls directly but, usually, via and alias. Aliases exists in memory and are checked before files within the PATH variable. We can see this by typing the command:
type ls
The output will show, on most distros that ls is aliased.
Here we can see that ls is aliases too:
ls --color=auto
This is great, as it means we can get the color listing of file types from ls without all that tiresome typing.
If we need to run ls temporarily without the alias, or the native ls command, we just preface it with the backslash, as such:
ls
This way we run the command as Linus intended and no sacrifice of performance to color . If we would like to remove the alias from this session we can issue the command:
unalias ls
or to remove all aliases:
unalias -a
This deletes the aliases from RAM until they are loaded again. This is normally in the login scripts, .bash_profile or .bashrc. If we want to create an alias for ourselves we can use the alias command to create aliases.
alias fd='find -maxdepth 1 -type d'
This alias, fd, would then run find listing directories only within our current directory. If wanted this to pemanent, and not just for the session we would probably want to add it to the .bashrc file within our home folder. If for whatever reason we do not want to run aliases we can disable in the BASH shell using the command:
shopt -u expand_aliases
-u inset the option -m turns it on again. We can check the current state by running:
shopt -p expand_aliases
Bash Built-ins
Built-in commands are those that are compiled into he shell themselves. These can exits in shells like bash and other shells and became an integral part of the shell itself. To list the internal commands to a bash or bourne shell type the command help.
For help on and individual built-in command the type help <command>
help umask
Some commands are listed both inside the shell binary as a built-in or internal command as well as a separate entity within the file-system, You may not be aware the that internal command is running rather than the one withing your PATH variable. You can use the command type to see if the command is built into the shell.
type <command>
Here we see that the command pwd is a built-in and unless we use the full path to the pwd binary, /bin/pwd the built in will be used. Switching between the two commands may be confusing when viewing the output as they behave differently; /bin/pwd has no option switches and behaves like the built-in pwd with the -p option. If run when in a symlinked directory /bin/pwd will show the actual target directory whereas pwd used on its own will show the symlink directory. Compare the two output’s from the following screenshot when the two pwd commands are run from within a directory that is a symlink (soft linked directory).
This is not normally an issue as most times you will run the comd just with its name so the built-in will run before the PATH variable is searched.
History
Most shells maintain the history of command already entered, this is usually persistent and stored within a file, in the case of BASH ~/.bash_history. The most simple form of history we can use in just the up arrow key where we can then see previous commands that we have issued, but this can be a little long winded and tedious. The normal BASH option for history expansion will be turned on by your shell, histexpand. This can be checked from your variable $SHELLOPTS . If histexpand does not appear you can turn it on with :
set -o histexpand
History expansion allows use of the ! mark to recall history
◉ !! for the last command
◉ !22 for line 22 in your history
◉ !v for the last command starting with v
◉ !$ to access the last argument used int he shell
This certainly makes the shell a little more usable but to go the whole way we need to use the keys ctrl + r to reverse search our history. This then could be
ctrl + r test
To search for test in out history, test does not have to be the first word or even a complete word, we are searching for the string test from the bottom of our history up, ctrl + b to go forward in the history. If we use cntrl + r and hit a match that we don’t want we can use ctrl + r again to search back from that point until we find the match we want. Hence ctrl + b so we can forward again if we go to far back.
There are variables that control the use of history in the shell and we look at setting HISTCONTROL to erasedups . This way each line will be unique in out history and duplicate lines will be deleted ; ensuring that we have a single occurrence of each command and option used.
Let the video explain all…….
0 comments:
Post a Comment