Linux Shell Basics 2 - Tsanderdev
Linux Shell Basics 2 - Tsanderdev
Remember: If you don’t know any terms, check out the glossary.
Environment Variables
Environment variables are simple pairs of a name and a value that are passed to each
command you run.
You can use the command env to print all current ones out.
You can define new ones with export NAME=VALUE .
You can also define exported variables for one command only: A=aaaa env will also list
the variable A .
Shell Variables
Shell variables are like environment variables, but are not passed to commands you run.
You can set them with NAME=VALUE .
To use a shell or environment variable, use $NAME , e.g. echo $HOME to print the home
directory of the user.
echo is another “command” that’s usually not a separate program, but build into the shell
for things like printing variables.
When you want to use 2 variables directly after each other, you have to also enclose them in
{} so the shell knows it’s not just one name: echo ${HOME}${USER}
One handy thing is that variables are replaced before anything about the command gets run:
LS="ls -l"
$LS ..
When using variables where you don’t know if the value contains spaces, always encase
them in "" , or you might get nasty surprises:
This will try to print the contents of the files my , super , cool , filename , with ,
spaces.txt , not my super cool filename with spaces.txt .
Sometimes you want the parts of the variable value to be treated as separate though:
ls_options="-a -l"
ls "$ls_options"
Redirection
To understand redirection, we have to make a small detour over file descriptors:
File Descriptors
Whenever a program opens a file, it gets a positive number back that references this file in
the operating system for the program. That number is a file descriptor.
The first 3 file descriptors have a standard meaning:
Programs can close file descriptors to make the number available for a new file and
duplicate a file descriptor to another number, so they reference the same actual file.
The shell exposes this in the form of I/O redirection.
Shell Redirection
Let’s say a program writes to the standard output, but you only want the error messages.
You could redirect the standard output away if you want to keep it.
Closing it would maybe make the program crash, as it expects to be able to write the
standard output (it’s standard, yknow?).
For this there are 2 special files:
/dev/null : Acts as an infinite sink for data. The operating system just discards any
data written to this file.
/dev/zero : An infinite source of 0 bytes. Writes are also discarded.
You can use FD<NAME to open the file descriptor FD for reading (piping data into FD) from
the file NAME and FD>NAME to make writes go to the file NAME (piping from FD to the file
NAME).
When writing, the file gets overwritten. If you want to append the data instead, use
FD>>NAME .
If FD is not given, 0 is assumed for < and 1 for > and >>.
You can also specify another file descriptor on the right side, but you have to use & so the
shell knows it’s not a filename: FD1<&FD2 .
With that knowledge we can now pipe away the pesky output into oblivion!
ls >/dev/null will print nothing, which is exactly what we were after in this case.
Another case is where you don’t want the error information and just the normal output:
COMMAND 2>/dev/null will discard the standard error for COMMAND.
And lastly, you may not want any output from a program:
Let’s say you have a copy of Mobby-Dick in the file mobby.txt and you want to find all lines
where “Mobby” is used and send them to a friend. Sadly, Emails don’t support such large
attachments.
You could do
grep Mobby mobby.txt > found.txt
gzip found.txt
If your friend wants to send you back the lines that also contain “whale”, they could do
and send you back the file without clobbering the directory with temporary uncompressed
files.
These are the main handy features for interactive shell use. The next post is about creating
shell scripts.
tsanderdev Blogs
u/tsanderdev