Description:Candidates should be able to interact with shells and commands using the command line. The objective assumes the Bash shell.
Key Knowledge Areas:
Use single shell commands and one line command sequences to perform basic tasks on the command line
Use and modify the shell environment including defining, referencing and exporting environment variables
Use and edit command history
Invoke commands inside and outside the defined path
Terms and Utilities:
bash
echo
env
export
pwd
set
unset
man
uname
history
.bash_history
What's A "Terminal?"
It's a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators we can use. Most Linux distributions supply several, such as: gnome-terminal, konsole, xterm, ... , It is also called Console.
What is "The Shell"?
Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell.
What is The "bash"?
On most Linux systems a program called bash acts as the shell program. bash stands for Bourne Again SHell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne
Besides, There are other shell programs that can be installed in a Linux system. These include: ksh, tcsh and zsh.
[root@centos7-1 ~]# ls -ls /bin/sh
0 lrwxrwxrwx. 1 root root 4 Oct 28 2017 /bin/sh -> bash
note: Historically the original /bin/sh Bourne shell would use $ as the normal prompt and # for the root user prompt This made it pretty easy to tell if you were running as superuser or not.
[root@centos7-1 ~]#
[root@centos7-1 ~]# su - user1
Last login: Mon Dec 10 11:44:47 EST 2018 on pts/0
[user1@centos7-1 ~]$
[user1@centos7-1 ~]$ su - root
Password:
Last login: Wed Dec 12 14:34:32 EST 2018 on pts/0
[root@centos7-1 ~]#
Standard Input, Standard Output and Standard Error
In general, a command (a program):
Gets data to process from standard input or stdin (default: keyboard).
Returns processed data to standard output or stdout (default: screen).
If program execution causes errors, error messages are sent to standard error or stderr (default: screen).
echo
echo is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file.
The syntax for echo is: echo [option(s)] [string(s)]
example : Input a line of text and display on standard output:
[root@centos7-1 ~]# echo Lets start learning Linux
Lets start learning Linux
The echo command has a couple of options. Normally, echo will append a trailing new line character to the output. Use the-noption to suppress this:
What makes a character special? If it has a meaning beyond its literal meaning, a meta-meaning, then we refer to it as a special character or metacharacters like : | & ; ( ) < > [ ] { } * ! ? ` ' " $ \ / #
pound sign (#)
Everything written after a pound sign (#) is ignored by the shell. This is useful to write a shell comment, but has no influence on the command execution or shell expansion.
[root@centos7-1 temp]# ls # ls /etc
zip-3.0-11.el7.x86_64.rpm zip.cpio
end of line backslash \
Lines ending in a backslash are continued on the next line. The shell does not interpret the newline character and will wait on shell expansion and execution of the command line until a newline without backslash is encountered.
[root@centos7-1 ~]# echo first line second line \
> 2nd line \
> 3rd line
first line second line 2nd line 3rd line
escaping special characters
The backslash character enables the use of control characters, but without the shell interpreting it, this is called escaping characters.
[root@centos7-1 ~]# echo hello | by
bash: by: command not found...
[root@centos7-1 ~]# echo hello \| by
hello | by
[root@centos7-1 ~]# echo hello ; by
hello
bash: by: command not found...
[root@centos7-1 ~]# echo hello \; by
hello ; by
Double quotes " "
Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, [with the exception of $, `, \, and, when history expansion is enabled, ! .The characters $ and ` retain their special meaning within double quotes].
[root@centos7-1 ~]# echo "hello | by"
hello | by
[root@centos7-1 ~]# echo "hello ; by"
hello ; by
Lets take a look at other options of echo command:
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version
output version information and exit
If -e is in effect, the following sequences are recognized:
\b backspace
\\ backslash
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
Use the-eoption to enable certain backslash escaped characters to have special meaning.
Escape sequence
Function
\a
Alert (bell)
\b
Backspace
\c
Suppress trailing newline (same function as -n option)
One of echo command usage is getting variable values with echo $VARIABLENAME command. We will see that.
[root@centos7-1 ~]# echo $LD_LIBRARY_PATH
Control Operators
Certain metacharacters or pairs of metacharacters also serve as control operators:|| && & ; ;; | ()
Some of these control operators allow you to create sequences or lists of commands.
Linux command chaining
Sometimes we would want to run multiple commands in succession or simultaneously. This allows us to automate a process that include several commands that may or may not depend on the result of the previous command.
Linux command chaining is the method of combining several different commands such that each of them can execute in succession based on the operator that separate them. The important part of chaining is the operators that we use to combine them. These operators determine how the commands execute.
Lets take a look at some of operators that are used mostly to combine different commands :
Semi-Colon (;) : The succeeding commands will execute regardless of the exit status of the command that precedes it.
Environment Variables are some special variables that are defined in shell and are needed by programs while execution. They can be system defined or user defined.
System defined variables are those which are set by system and are used by system level programs.
The whole concept of setting and un-setting environment variables revolves around some set of files and few commands and different shells.
an environment variable can be in three types:
1. Local Environment Variable :One defined for the current session. These environment variables last only till the current session, be it remote login session, or local terminal session. These variables are not specified in any configuration files and are created, and removed by using a special set of commands.
2. User Environment Variable : These are the variables which are defined for a particular user and are loaded every time a user logs in using a local terminal session or that user is logged in using remote login session. These variables are typically set in and loaded from some configuration files which are present in user’s home directory.
3. System wide Environment Variables :These are the environment variables which are available system-wide, i.e. for all the users present on that system. These variables are present in system-wide configuration files present in /etc directories and files.These variables are loaded every time system is powered on and logged in either locally or remotely by any user.
env
By default, "env" command lists all the current environment variables.
These varibales make working with shell easier. Some of most important ones are:
HOSTNAME:The name of the your computer.
SHELL: This describes the shell that will be interpreting any commands you type in. In most cases, this will be bash by default, but other values can be set if you prefer other options.
TERM: This specifies the type of terminal to emulate when running the shell. Different hardware terminals can be emulated for different operating requirements. We usually won't need to worry about this though.
USER: The current logged in user.
LS_COLORS: This defines color codes that are used to optionally add colored output to the ls command. This is used to distinguish different file types and provide more info to the user at a glance.
MAIL: The path to the current user's mailbox.
PATH: A list of directories that the system will check when looking for commands. When a user types in a command, the system will check directories in this order for the executable.
[root@centos7-1 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7-1 ~]# which firefox
/bin/firefox
[root@centos7-1 ~]# type firefox
firefox is /bin/firefox
[root@centos7-1 ~]# whereis firefox
firefox: /usr/bin/firefox /usr/lib64/firefox /etc/firefox /usr/share/man/man1/firefox.1.gz
For running other scripts or commands we can add them to path, or use full path or relative path (. and ..)
LANG: The current language and localization settings, including character encoding.
HOME: The current user's home directory.
_: The most recent previously executed command.
OLDPWD: The previous working directory. This is kept by the shell in order to switch back to your previous directory by running cd -.
The exit code of the previous command is stored in the shell variable $?. Actually $? is a shell parameter and not a variable, since you cannot assign a value to $?.
[root@centos7-1 ~]# touch my.txt
[root@centos7-1 ~]# echo $?
0
To see the value of a variable use echo $VARIABLENAME :
To set a Local Variable we create a local variable VAR1 and set it to any value( Historically it is recommended to use upper case for variables but you are free to use what ever case you want):
Any process can be a parent and child process at the same time. The only exception is the init process, which is always marked with PID ( process ID ) 1. Therefore, init is a parent of all processes running on your Linux system.
Any process created will normally have a parent process from which it was created and will be considered as a child of this parent process.
echo $$ print a PID for a current shell
When creating a new child process (a sub shell in our example) an export command simply ensures that any exported variables in the parent process are available in the child process.
We can terminate a shell using the exit command or simply press ctrl+d keys.
( ) : parentheses really put the command in a subshell. try(exit)
export
export command is one of the bash shell builtin commands, marks an environment variable to be exported to child-processes, so that the child inherits them.
[root@centos7-1 ~]# VAR2="Linux is Fun"
[root@centos7-1 ~]# echo $VAR2
Linux is Fun
[root@centos7-1 ~]# export VAR2
[root@centos7-1 ~]# echo $$
24328
[root@centos7-1 ~]# bash
[root@centos7-1 ~]# echo $$
24722
[root@centos7-1 ~]# echo $VAR2
Linux is Fun
Syntax:export [-fn] [name[=value] ...] or export -p
The export command is fairly simple to use as it has straightforward syntax with only three available command options.
-p List of all names that are exported in the current shell
-n Remove names from export list
-f Names are exported as functions
setis a shell built-in that (If no options or arguments are supplied) displays all shell variables, not only the environment ones, and also shell functions, which is what you are seeing at the end of the list.
Long story short: set can see shell-local variables, env cannot.
As we have mentioned, shells can have variables of 2 types: locals, which are only accessible from the current shell, and (exported) environment variables, which are passed on to every executed program.
Since set is a built-in shell command, it also sees sees shell-local variables (including shell functions). env on the other hand is an independent executable; it only sees the variables that the shell passes to it, or environment variables
In bash like in any Bourne-like shell, set is the command to set options (shell configuration settings like -f, -C, -o noclobber...) and positional parameters ($1, $2...).
set [--abefhkmnptuvxBCEHPT] [-o option-name] [argument …]
set [+abefhkmnptuvxBCEHPT] [+o option-name] [argument …]
Use - and + signs for enabling and disbaling options.Tryhelp set | less .
To see the current shell options use echo $- :
[root@centos7-1 ~]# echo $-
himuBH
For example -u treat unset variables as an error when substituting:
We can use set -o without any option to get the current state of shell options :
[root@centos7-1 ~]# set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
set +o turns off specified option and set -o turns it on .For example lets disable and enable History Storing with -o option:
[root@centos7-1 ~]# set +o history
[root@centos7-1 ~]# set -o | grep history
history off
[root@centos7-1 ~]# set -o history
[root@centos7-1 ~]# set -o | grep history
history on
One of important options you might be asked for in the exam is noclobber, The noclobber option prevents you from overwriting existing files with the > operator (Discussed in next courses).
set is not for setting variables. Do not make mistake!
Set has lots of options which are used to change bash behaviour.
unset
unset command is used to unset any local environment variable temporarily:
[root@centos7-1 ~]# VAR5="Linux Linux Linux"
[root@centos7-1 ~]# echo $VAR5
Linux Linux Linux
[root@centos7-1 ~]# unset VAR5
[root@centos7-1 ~]# echo $VAR5
bash: VAR5: unbound variable
[root@centos7-1 ~]# unset PATH
[root@centos7-1 ~]# ls
bash: ls: No such file or directory
So set is not set, but unset is unset.
We can also use env VARNAME=VALUE and env -u VARNAME to set and unset a variable.
Understanding Bash History
History of all commands which are executed by all users are stored.History is maintained both in ram and in a file.bash_history. History which is maintained in ram, manipulated with the commandhistoryand environment variables. It is important to note that the history contained in ram is only written after a user logs out of his or her session.
history
history shows the current content of Bash's history list in memory for the current session.
[root@centos7-1 ~]# history
1 dhclient -r
2 dhclient
3 seastatus
4 sestatus
5 vim /etc/sysconfig/selinux
6 setenforce 0
7 sestatus
8 reboot
9 cd /var/lib/ldap/
10 ls
11 ls -la
12 ls -lrth
13 ping 8.8.8.8
14 dhclient -r
15 shcli
16 dhclient
17 ping 8.8.8.8
18 sestatus
19 yum install openldap openldap-servers openldap-clients.x86_64
20 systemctl status slapd.service
<output has been trancuated>
How many lines are stored in RAM version of History list? It is defined by HISTSIZE variable.
HISTSIZE : This variable contains the maximum number of lines that be contained in the in-ram version of history.
and that would be 1000 lines! to see just 5 lines for example usehistory 5 .
Repeat previous command quickly(3 methods)
Use the up arrow to view the previous command and press enter to execute it.
Type !! and press enter from the command line.(or !!number )
Press Control+P will display the previous command, press enter to execute it
Search the history(3 methods)
Control+R: Press Control+R and type the keyword.
!string Refers to the most recent command starting with string.
!?string? Refers to the most recent command containing string (the ending ? is optional).
Clear all the previous history : Use history -c option.
~/.bash_history
When user closes shell , Bash will save its history list to the disk by appending the contained entries to his/her ~/.bash_history hidden file. ~/.bash_history is controlled by some variables:
HISTFILE This variable contains the location of the history file. When bash is logged out of, the contents of the history command will be written to this file.
HISTFILESIZE The variable contains the maximum number of lines that may be in the history file. When bash writes to the history file, the oldest commands that go over this maximum number will be deleted.
it acts the same as HISTSIZE.
HISTCONTROL This variable contains instructions for what should be ignored when added to the history file. has four settings:
Setting Name Setting Effect
Ignorespace When this setting is used, any line that begins with one or more spaces will not be added to the history.
Ignoredups When this setting is used lines that match the previous entry in the history are not saved.
Ignoreboth When this setting is used it implies the conditions of both ignoredups and ignorespace.
Not Defined If HISTCONTROL is not defined no conditions will be applied before the line is entered into the history
To see the .bash_history hidden file we should use ls with -a switch:
[root@centos7-1 ~]# ls -la | grep .bash_history
-rw-------. 1 root root 9779 Dec 9 14:29 .bash_history
[root@centos7-1 ~]# cat /home/user1/.bash_history
ls
exit
He/she has done nothing:). .bash_history by default keeps 500 or 1000 of command which has been used.So we can go back to some thing we have done long time ago. history -c does not clear that and it remains after user log off, so if you don't like it you should remove it.
uname
uname command without any switch will print system information :
[root@centos7-1 ~]# uname
Linux
Its syntax is like uname [OPTION] ... .For example-a, --allPrints all information:
[root@centos7-1 ~]# uname -a
Linux centos7-1 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
If -a (--all) is specified, the information is printed in the following order of individual options:
-s, --kernel-name Print the kernel name.
-n, --nodename Print the network node hostname.
-r, --kernel-release Print the kernel release.
-v, --kernel-version Print the kernel version.
-m, --machine Print the machine hardware name.
-p, --processor Print the processor type, or "unknown".
-i, --hardware-platform Print the hardware platform, or "unknown".
-o, --operating-system Print the operating system.
--help Display a help message, and exit.
--version Display version information, and exit.
Processor information (-p) and Hardware-Platform(-i) are omitted if they are unknown.
man
In Unix-like operating systems, a man page (in full manual page) is a documentation for a terminal-based program/tool/utility (commonly known as a command). It contains:
the name of the command
syntax for using it
a description
options available
author
copyright
related commands and ... .
To read a manual page for a Unix command, a user can type:
man <command_name>
For example try man man :
By default, man typically uses a terminal pager program such as more or less to display its output.
The manual is generally split into eight or nine numbered sections, organized as follows :
Executable programs or shell commands
System calls (functions provided by the kernel)
Library calls (functions within program libraries)
Special files (usually found in /dev)
File formats and conventions eg /etc/passwd
Games
Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
System administration commands (usually only for root)
Kernel routines [Non standard]
To make man display manual page from specific sections use man [section-num] [command/tool name] :
man 1 echo
man will search for the desired manual pages within the index database caches. So there is no need to remember section numbers for manual entries .
mandb
mandb is used to initialise or manually update index database caches
that are usually maintained by man. The caches contain information
relevant to the current state of the manual page system and the
information stored within them is used by the man-db utilities to
enhance their speed and functionality.
the cache consistency check can be slow on systems with many manual pages installed, so mandb is not performed by default, and system administrators may wish to run mandb every week or so to keep the database caches fresh.
[root@centos7-1 ~]# mandb
Purging old database entries in /usr/share/man...
mandb: warning: /usr/share/man/man8/fsck.fat.8.manpage-fix.gz: ignoring bogus filename
Processing manual pages under /usr/share/man...
Purging old database entries in /usr/share/man/hu...
Processing manual pages under /usr/share/man/hu...
Purging old database entries in /usr/share/man/de...
Processing manual pages under /usr/share/man/de...
Purging old database entries in /usr/share/man/ja...
...
0 man subdirectories contained newer manual pages.
0 manual pages were added.
0 stray cats were added.
0 old database entries were purged.
Most useful man command options:
-f, --whatisDisplay a short description from the manual page
[root@centos7-1 ~]# man -f ls
ls (1) - list directory contents
ls (1p) - list directory contents
-w, --where, --location Don't actually display the manual pages, but do print of the source off files
[root@centos7-1 ~]# man -w ls
/usr/share/man/man1/ls.1.gz
-k, --apropos Equivalent to apropos. Search the short manual page descriptions for keywords and display any matches.
[root@centos7-1 ~]# man -k echo
echo (1) - display a line of text
echo (1p) - write arguments to standard output
fcping (8) - Fibre Channel Ping (ELS ECHO) tool
l2ping (1) - Send L2CAP echo request and receive answer
lessecho (1) - expand metacharacters
pam_echo (8) - PAM module for printing text messages
ping (8) - send ICMP ECHO_REQUEST to network hosts
ping6 (8) - send ICMP ECHO_REQUEST to network hosts
apropos
apropos command is used to search and display a short man page description of a command/program as follows.
[root@centos7-1 ~]# apropos echo
echo (1) - display a line of text
echo (1p) - write arguments to standard output
fcping (8) - Fibre Channel Ping (ELS ECHO) tool
l2ping (1) - Send L2CAP echo request and receive answer
lessecho (1) - expand metacharacters
pam_echo (8) - PAM module for printing text messages
ping (8) - send ICMP ECHO_REQUEST to network hosts
ping6 (8) - send ICMP ECHO_REQUEST to network hosts