Home... Help... Search... Computers...
A Brief Introduction to the BASH Shell and Linux
I love command line interfaces. GUIs are fine, and I use them a lot, but I still love and use the command line power-tools. They just make some tasks much much easier. My background includes command line interfaces on DOS, VAX-VMS, PDP11-RSX, Unix and Linux machines, as well as building and caring for a large Windows/Novell and later Windows/Active Directory network.
BASH, cutely named the "Bourne Again Shell" being descended from the earlier Bourne Shell of early Unix, is my preference, purely for historical reasons. My first foray into Linux was because I wanted to use the free "Big Brother" system monitor on my company's expanding network, and it was largely built up of BASH scripts. "Shell" is essentially Unix-speak for a command line interface, where you can type and run commands, or put strings of commands in a script and then run that.
In the last years of my career I created an entire web-based real-time information system for tank inventories and movements (I worked for an Oil Refinery) composed entirely of BASH scripts. Of course, the scripts used a multitude of UNIX utilities and open-source software to do their work. The web-pages had graphs of tank levels, flow-rates, colour-changing alarms, and dynamically generated flow diagrams. BASH/UNIX is powerfulXXX
I've published a number of BASH scripts on this website, eg reading data from a power meter and reading data from an Arduino micro-controller, and it occurred to me that others who might be interested in what I was trying to achieve might not have much familiarity with either BASH or command line interfaces. I'll also throw in a bit on similarities with the DOS/Windows command line.
A command line interface without input/output redirection would be a bit like a horse with no legs. What's it all about?
All command line tools in UNIX/Linux have the concept of redirection embedded in them. UNIX supports what is called standard output and standard input. By default standard output is the screen (typically in a terminal window) and standard input is the keyboard. But we can redirect either with ease. For example, if you want a list of all the files in a given folder, and want the list in a text file (eg, so you can email it to someone), it is very easy. In UNIX, the command is ls, so we can just
ls > files.txt
The ">" says send the output of the ls command, not to screen, but to a file named "files.txt"
Another example is the tr command, which is used to translate characters in a file. Eg, if we want to turn all lower-case letters to upper-case
tr '[a-z]' '[A-Z]' < file1.txt > file2.txt
Here tr gets its input from file1.txt and sends its output to file2.txt
A final example is the "|" operator, called the "pipe". It allows you to string together a series of commands, taking the output of the first and "piping" it directly, as input, into the second command, etc. Say you want to find out what folders (directories) are in a given folder. ls -l (ie, "long" format) shows a lot of information about files and folders. If you try it you will see that directories always have the characters "drw" in the left hand "permissions" field. We can use this to get a listing of folders, filtering out the files.
ls -l | grep drw
We are piping the output of ls -l to the grep (search) command, which then outputs only lines containing "drw", ie, all the folders. If there are so many folders that the list scrolls off the screen, you can add
ls -l | grep drw | more
to it, sending the list to the more command, which displays one screen at a time.
Table of Useful Bits | |||
---|---|---|---|
Item | Example | Notes | DOS Equivalent? |
> | ls > files.txt | send a list of files to a text file, overwriting it if it already exists | dir > files.txt |
>> | ls >> files.txt | send a list of files to a text file, adding them to the file if it exists already | dir >> files.txt |
< | tr -d 'X' < file.txt | delete all "X" characters from a file | it exists, but I can't think of an example |
| |
ps ax | grep minicom |
send a list of all processes on the computer to grep to see if anyone is using minicom. Note ps is not part of bash, just part of UNIX / Linux | type file.txt | more |
cat | cat files.txt | send the contents of the file to the screen | type files.txt |
awk | awk '{print $3}' folders.txt | read folders.txt one line at a time and output just the third field (as delimited by whitespace. awk is not part of bash, just universal in UNIX /Linux systems | none |
echo | echo "Hi there" > file.txt | put the text into a file | echo "Hi there" > file.txt |
head | head -3 files.txt | output just the first three lines of the file | none |
tail | tail -3 files.txt | output just the last three lines of the file | none |
cp | cp file1 file2 | copy file1 to file2 | copy file1 file2 |
rm | rm file1 | remove file1 | del file1 |
mv | mv file2 old | move the file to the old folder | move file2 old |
mv | mv file1 file3 | rename the file | rename file1 file3 |
` | DATE=`date` | (called the "back-tick") get the output of the date command and store it in the variable "DATE" | none |
Home... Help... Search... Computers...
This page tardus.net/briefIntro2BashShell.html Last refreshed: 04 Oct 2023
Contact me, "Tardus" Copyright powered by txt2tags