Basic shell commands

Print Text into the standard output

In case you have a variable, use:
echo $variablename

In case you want to print a text prompting a user, use:
echo This text will be printed on the screen

or you may use if you need to space at the end of the line:
echo "This text will be printed on the screen: "

Read Variables from standard input

Read a single variable:

read something
when executing the above shell command, the terminal will pause and wait for user’s input.

Once the user inputs some text and presses enter, the text will be stored in a variable name called $something.

Read multiple variables:

read something1 something2 something3

If user inputs: word1 word2 word3 then it is equivalent to,
$something1 =”word1″
$something2 =”word2″
$something3 =”word3″

If user inputs: word1 then it is equivalent to,
$something1 =”word1″
$something2 =””
$something3 =””

If user inputs: word1 word2 word3 word4 word5 then it is equivalent to,
$something1 =”word1″
$something2 =”word2″
$something3 =”word3 word4 word5″

Output Redirection

the “>” redirects the output into a new file / replaces existing file.

ls -ltr > filelist

The above would save the output of the “ls -ltr” into the filelist file.

the “>>” Appends the standard output of a command into existing file.


echo the above was a list of files found inside the directory: >> filelist
pwd >> filelist

The above would append the output of the first echo statement and the folder path returned by pwd into filelist.

Pipeline commands

Pipeline is the process of using the standard output of one shell program as the standard input of another. This can be done by the usage of the character “|”

program1 | program2

In the above case, the output of program1 is used as input to program2.

Getting an input from a file

To use a file as an input to a script or program requiring standard input, use the “<" character.

Example:
Read filedata < filelist

The above command would place the content of the first line of filelist into $filedata variable.

Advertisements