Linux - Unit-1-Chp-2
Linux - Unit-1-Chp-2
Linux - Unit-1-Chp-2
Command Line
Variables:
A shell variable is a common value that is used often by the
shell and commands that work from that shell, and it is
stored with a given name. An example of such a variable is
PATH, which stores a list of directories that should be
searched when a user enters a command. To refer to the
contents of a variable, prepend a $ sign before the name of
the variable. For example, the command echo $PATH would
display the contents of the current search path that Bash is
using.
completed.
Redirection
Whereas piping is used to send the result of a command to another command, redirection sends the
result of a command to a file. While this file can be a text file, it can also be a special file, such as a
device file.
In Exercise 2.3, first you’ll use the ps aux command without redirection. The results of the command
will be written to the terminal window in which you are working. In the next step, you’ll redirect the
output of the command to a fi le. In the fi nal step, you’ll display the contents of the file using the
less utility.
EXERCISE 2.3
Redirecting Output to a File
1. From a console window, use the command ps aux. You’ll see the output of the command on the
current console.
2. Now use ps aux > ~/psoutput.txt. You don’t see the actual output of the command, because it is
written to a fi le that is created in your home directory, which is designated by the ~ sign.
3. To show the contents of the file, use the command less ~/psoutput.txt.
Do not use the single redirector sign (>) if you don’t want to overwrite the content of existing files.
Instead, use a double redirector sign (>>). For example, who > myfile will put the result of the who
command (which displays a list of users currently logged in) in a file called myfile. If then you want to
append the result of another command, for example the free command (which shows information
about memory usage on your system), to the same fi le myfile, then use free >> myfile.