Bash
Bash is the default shell environment in Linux; that is, it is the interface in which you will be interacting with your Linux server. Bash is a derivative of sh, one of the first shells. Other popular shells include csh and tcsh, shells with c-like syntax for scripting, and zsh a bash-like shell which focuses on extending the capabilities of the shell environment.
Displaying a Value
To display a value at the shell prompt, use the command echo.
$ echo "Hello World" # displays Hello World
Hello World
$
Note: In examples, code written at the prompt is conventionally denoted by a line starting with a currency symbol. Lines without a currency symbol represent output.
Seeing the contents of a file
If you want to see the contents of a file, use the cat command.
$ cat myfile.txt
Hello World
$
cat is one of a number of useful Linux command-line binaries, the rest of which we will see later.
Working Directory
Whenever you are interacting with the shell, you will be executing commands from a working directory. To see the current working directory, run the command pwd (path to working directory). To change the working directory, run the command cd (change directory).
$ pwd
/home/todd
$ cd projects
$ pwd
/home/todd/projects
$ cd ./ # recall that . is the current directory
$ pwd
/home/todd/projects
$ cd ../ # recall that .. is the next directory up in the filesystem
$ pwd
/home/todd
$
If you run commands that interact with the filesystem (e.g. ones that create or edit files), they will be saved in your current working directory.
Variables
Bash supports the use of variables. There are system-defined variables, and you can also define your own custom variables.
Defining and Accessing Variables
$ MYVARIABLE="Hello World" # assigns the value Hello World to the variable MYVARIABLE
$ echo $MYVARIABLE # notice that you need to put a currency symbol in front of the variable in order to access its value
Hello World
$ export $MYVARIABLE # allows MYVARIABLE to be accessed in child processes (e.g., in a program you call from the shell)
$ export MYVARIABLE="Hello Moon" # a shortcut for defining a variable and exporting it to subprocesses
$ set # displays a list of all currently set variables
MYVARIABLE=Hello World
$
System Variables
Bash comes pre-loaded with certain environment variables. Some of the variables with which you may find yourself interacting include:
- PATH: search path for the commands
- PWD: name of the current directory
- SHELL: type of shell
- TERM: type of the terminal
- USER: the account name
- HOME: the user's home directory
- PS1: the prompt at command line
- $$: the process id of current shell
- $RANDOM: a random value
- $?: the return value of the last command
- $_: the last argument of the previous command
- $#: where # is a number, the value of the #th argument
- IFS: input field separator
Try echoing some of the system variables to examine your current environment.
Running Programs
To run an executable file, simply enter its filename into the shell prompt:
$ /usr/bin/perl -v # runs the binary executable located at /usr/bin/perl with the flag -v
This is perl 5, version 12, subversion 3 (v5.12.3)
$ ../mydir/myprogram # runs the binary located one level up in the file system, then in mydir/myprogram
You just ran myprogram!
$
Programs in your PATH
Many commonly-used executable binaries are located in /bin, /usr/bin, and similar directories. In order to avoid typing paths to these directories every time you want to execute a command, you define these directories in your PATH system variable:
$ echo $PATH # displays the current value of the PATH variable
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
$ PATH=$PATH:/my/favorite/bin # adds a directory to your PATH variable
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/my/favorite/bin
$
Notice that the different PATH directories are separated by colons. Now, when you execute a command, Bash will scan all of the directories in your PATH variable. To see the path to the binary that Bash found, use the which command.
$ perl -v
This is perl 5, version 12, subversion 3 (v5.12.3)
$ which perl
/usr/bin/perl
$
Note: it is unwise to have . in your PATH. Instead, if you want to run an executable in the current directory, do so by calling ./myprogram:
$ ./myprogram
You just ran myprogram!
$ myprogram
-bash: myprogram: command not found
$
Foreground and Background Processes
A program runs in the foreground (unless it detaches itself from the terminal) by default. You can run a program in the background by adding & at the end of the command (after arguments). In this case, the shell would fork a process for that program and enable the command prompt back for input. At any time, jobs command can be used to see the processes running at the background. fg command brings the specified process back to foreground. A program running in the foreground can be stopped by typing ctrl-c in most cases. Typing ctrl-z interrupts a program running in the foreground. If a program is interrupted, it will not continue executing until it is resumed. An interrupted program can be brought back to foreground by fg, or it could be send to background by bg.
$ ./myprogram
You just ran myprogram!
I am taking a long time to run.
^C
$ jobs
$ ./myprogram
You just ran myprogram!
I am taking a long time to run.
^Z
[1]+ Stopped ./myprogram
$ jobs
[1]+ Stopped ./myprogram
$ bg
[1]+ ./myprogram &
$ jobs
[1]+ Running ./myprogram &
$ fg
^C
$ jobs
$ ./myprogram &
[1] 64741
$ jobs
[1]+ Running ./myprogram &
$
Killing Processes
A process can be killed by using the kill command: kill process-number
In some cases the kill signal can be ignored, so it may be necessary to force kill the program by sending an absolute KILL signal: kill -9 process-number
The current processes can be listed using the ps command.
$ ps # list currently running processes in the current shell
PID TTY TIME CMD
19107 ttys000 0:00.75 -bash
1873 ttys001 0:00.05 -bash
57267 ttys002 0:00.20 -bash
50721 ttys003 0:00.55 -bash
$ ps -eaf # list all currently running processes
UID PID PPID C STIME TTY TIME CMD
0 1 0 0 31Dec00 ?? 3:24.45 /sbin/launchd
0 19106 327 0 1Aug12 ttys000 0:00.03 login -pfl sffc /bin/bash -c exec -la bash /bin/bash
501 19107 19106 0 1Aug12 ttys000 0:00.75 -bash
0 1872 327 0 31Jul12 ttys001 0:00.02 login -pfl sffc /bin/bash -c exec -la bash /bin/bash
501 1873 1872 0 31Jul12 ttys001 0:00.05 -bash
0 57266 327 0 Mon05AM ttys002 0:00.08 login -pfl sffc /bin/bash -c exec -la bash /bin/bash
501 57267 57266 0 Mon05AM ttys002 0:00.20 -bash
0 64747 57267 0 9:58AM ttys002 0:00.00 ps -eaf
0 50720 327 0 Fri12AM ttys003 0:00.03 login -pfl sffc /bin/bash -c exec -la bash /bin/bash
501 50721 50720 0 Fri12AM ttys003 0:00.55 -bash
$
Directing Output
A program's standard output can be send to a file by typing >filename at the end. Similarly, >> appends to a file. In Linux, there are three default file handlers, standard input or STDIN, standard output or STDOUT, and standard error or STDERR. STDOUT has a file handler number 1 and STDERR has a number of 2. In bash, you can direct either of these handlers to a file. You can also redirect one file handler to another.
$ ./myprogram >filename.txt # redirects all output to filename.txt
$ cat filename.txt
You just ran myprogram!
$ ./myprogram >>filename.txt # appends the output to filename.txt
$ cat filename.txt
You just ran myprogram!
You just ran myprogram!
$ ./myprogram 1>filename.txt # redirects the standard output to filename.txt
$ cat filename.txt
You just ran myprogram!
$ ./myprogram 2>filename.txt #redirects the error output to filename.txt
You just ran myprogram!
$ ./myprogram 2>&1 # STDERR is redirected to STDOUT
You just ran myprogram!
$
Output of one program can be redirected to the input of another program using pipes.
$ ./program1 | ./program2 # send program1's output as an input to program2
You just ran program2 with the input: You just ran program1!
$
Redirection is possible for STDIN too. A program can get its input by redirecting STDIN using <
$ ./myprogram < inputfile.txt
You just ran myprogram with input from inputfile.txt!
$
Finally, ` (a backtick) can be used to capture the output of a program, and use it as a string such as in setting a variable
$ MYVARIABLE=`./myprogram`
$ echo $MYVARIABLE
You just ran myprogram!
SUDO
Some commands require root privileges to run. In order to run a command as root without logging in as root, use sudo.
$ yum install lynx
You need to be root to perform this command.
$ sudo yum install lynx
[sudo] password:
.....
Complete!
$
Automatically Running Programs
You will often find it useful for binaries to be executed at predefined intervals, certain days of the week, or at startup. Linux provides you with the tools you need to make these configurations.
Scheduled Programs in Cron
Cron is a system service that will run programs in a periodic manner. For more details on how to configure cron, see the Cron guide.
Programs at Startup
When a Linux system boots there are a series of scripts that are called to start up system processes, daemons, and other programs (such as SSH servers, web servers, database programs, etc). The simplest way to add something to the boot process is to add it to /etc/rc.local, which is a script that is called automatically at the very end of the boot process. Simply write a script that does what you want and then call it from with in /etc/rc.local to ensure that your script is called at the end of the boot process.
You can also add scripts which run at different times during the boot process. The way to do this varies by Linux distribution. For Fedora, see http://www.yolinux.com/TUTORIALS/LinuxTutorialInitProcess.html (specifically the section entitled Init Script Activation).
Shell Scripting
Programs can be scripted using Bash. For more information, see Shell Scripting.