Table of Content

Weight: 4

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

echo

echo -e "No new line\c"
oldhorse@dclab-u1504s:~$ echo -e "No new line\c"
No new lineoldhorse@dclab-u1504s:~$

env

USER
UID
HOME
PWD
SHELL
$  process id
PPID parent process id
?  exit code

oldhorse@dclab-u1504s:~$ echo $USER $UID $PPID $$ $?
oldhorse 1000 883 884 0

oldhorse@dclab-u1504s:~$ ps -p $$ -o "pid ppid cmd"
  PID  PPID CMD
  884   883 -bash

exec

run another program that replaces current shell

oldhorse@dclab-u1504s:~$ echo $$
884
oldhorse@dclab-u1504s:~$ bash
oldhorse@dclab-u1504s:~$ echo $$
910
oldhorse@dclab-u1504s:~$ exec sh
$ echo $$
910
$ exit
oldhorse@dclab-u1504s:~$ echo $$
884

uname

oldhorse@dclab-u1504s:~$ uname 
Linux
oldhorse@dclab-u1504s:~$ uname -a
Linux dclab-u1504s 3.19.0-15-generic #15-Ubuntu SMP Thu Apr 16 23:32:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
oldhorse@dclab-u1504s:~$ uname -s
Linux
oldhorse@dclab-u1504s:~$ uname -n
dclab-u1504s
oldhorse@dclab-u1504s:~$ uname -r
3.19.0-15-generic
oldhorse@dclab-u1504s:~$ uname -v
#15-Ubuntu SMP Thu Apr 16 23:32:37 UTC 2015
oldhorse@dclab-u1504s:~$ uname -m
x86_64
oldhorse@dclab-u1504s:~$ uname -o
GNU/Linux

history

~/.bash_history   history file

history -c # clear history 

!! last cmd
!N Nth cmd
!-N Nth back in history
!string  starting with string
!?string?  most recent cmd has string

path

oldhorse@dclab-u1504s:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

- if multi version pkg instaled, always 1st one is valid in path
- never include current directory . in root path

set , unset

  • set – define variable
  • unset – undefine variable

cd

cd
cd $HOME
cd -     # cd $OLDPWD
oldhorse@dclab-u1504s:~$ cd /tmp
oldhorse@dclab-u1504s:/tmp$ cd
oldhorse@dclab-u1504s:~$ env|grep OLD
OLDPWD=/tmp
oldhorse@dclab-u1504s:~$ cd -
/tmp
oldhorse@dclab-u1504s:/tmp$ cd ~
oldhorse@dclab-u1504s:~$ pwd
/home/oldhorse

man

whatis search man name 
apropos search keyword in man 

oldhorse@dclab-u1504s:~$ whatis reboot
reboot (8)           - Halt, power-off or reboot the machine
reboot (2)           - reboot or enable/disable Ctrl-Alt-Del

oldhorse@dclab-u1504s:~$ apropos reboot
grub-reboot (8)      - set the default boot entry for GRUB, for the next boot only
halt (8)             - Halt, power-off or reboot the machine
poweroff (8)         - Halt, power-off or reboot the machine
reboot (2)           - reboot or enable/disable Ctrl-Alt-Del
reboot (8)           - Halt, power-off or reboot the machine
rescan-scsi-bus.sh (8) - script for adding and removing SCSI devices without rebooting
shutdown (8)         - Halt, power-off or reboot the machine
systemd-reboot.service (8) - System shutdown logic
oldhorse@dclab-u1504s:~$ 

Quiz questions

1. What command will bash attempt to run for the following example:
% hello help me type
2. What is the command to create an environment variable called EXAMPLE and set its value to "Hello there"?
3. Why will the following code print a blank line?
EXAMPLE="Hello there"
echo "$example"
4. What is the effect of having a "." in the PATH environment variable, and why is this not good practice?
5. How do you set an environment variable (MYNAME) to contain the contents of a file (/etc/HOSTNAME)?
6. Write a command that will copy the files and subdirectories from /etc to ~/etcbackup.

Answers to quiz questions

1. bash runs the command hello with the first parameter “help” the second “me” and the third “type” .
2. The command is EXAMPLE="Hello there"
export EXAMPLE="Hello there" ??
3. Environment variable names are case sensitive.
4. “.” is the current directory. This means that the shell will search for commands in the current directory. It is not good practice, since the current directory may include undesirable files that you do not necessarily want to run.
5. To set the variable and display its value:
MYNAME="$(cat /etc/HOSTNAME)"
echo "$MYNAME"
6. This will do it ...
cp -a /etc ~/etcbackup