Linux: Piping, Alias, Variables & Filters
Linux: Piping, Alias, Variables & Filters
Linux: Piping, Alias, Variables & Filters
• The more command takes the output of $ ls -l as its input. The net
effect of this command is that the output of ls -l is displayed one
screen at a time.
• The pipe acts as a container which takes the output of ls -l and gives it
to “more” as input.
• Previous command is equivalent to the sequence of commands listed
below
$ ls -l -> temp
more -> temp (or more temp)
[contents of temp]
rm temp
• 2) Use sort and uniq command to sort a file and print unique values
$ sort record.txt | uniq
• 4) Use ls and find to list and print all lines matching a particular
pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;
• This command select files with .txt extension in the given directory
and search for pattern like “program” in the above example and print
those lines which have program in them.
• 5) The cat, grep, tee and wc commands are used to read the
particular entry from user and store in a file and print line count.
$ cat result.txt | grep “Neha Sharma" | tee file2.txt | wc -
• Local variables
• The Linux system also defines some standard local environment
variables for you by default.
• To view the global and local variables for the shell you are running
and available to that shell, type the set command.
• Declare Local Variables
• To declare your own environment variables directly from the shell,
type variable Name you want, followed by an equal sign and the
variable value WITHOUT any spaces:
mysite=likegeeks
• And to print the variable value, use the echo command:
echo $mysite
• What if your variable is more than one word; maybe a long string, you
can put the string between single quotations:
• mysite='likegeeks is a website that offers tech tutorials for geeks'
• And if we type echo $mysite
• If you forget the single quotation, the shell will assume that the
second word is another command and will show an error.
• The use of lower case characters for self defined variables (not
uppercase) is recommended NOT required, this helps you
distinguishing your environment variables from the system
environment variables.
• Once you have set your local variable, it will be visible in the currently
running shell scope and that means if you start another shell window
the variable will not be available in that new window.
• Declare Global Variables (Export variables)