This document explains Linux filters, pipes, and variables. It covers how to use filter commands, chain them with pipes, create and manage shell variables and work with environment variables for extended scope.
This document explores Linux filter commands and how they transform input data into output data. It explains how to chain multiple commands using pipes to create powerful command sequences. Additionally, it covers both shell variables and environment variables, including their scope, creation, usage, and management techniques.
Filters are shell commands or programs that take input from standard input (usually the keyboard) and send their output to standard output (usually the terminal). They function as transformers that convert input data into a different form of output data.
Several commands in Linux operate as filters, including:
| Command | Description |
|---|---|
wc | Counts lines, words, and characters |
cat | Concatenates and displays file contents |
more | Displays output one screen at a time |
head | Displays the beginning of a file |
sort | Sorts input lines |
grep | Searches for patterns in text |
The pipe command, represented by the vertical bar symbol (|), significantly extends shell functionality by allowing users to chain multiple filter commands together. This creates a pipeline where the output of one command becomes the input for the next command.
The syntax for using pipes is:
command1 | command2 | command3
In this structure:
command1 becomes input for command2command2 becomes input for command31ls | sort -r
This command pipes the output of ls (which lists directory contents) to the sort command with the -r option, resulting in a reverse-sorted list of directory contents.
Shell variables are variables limited in scope to the shell in which they were created. Different shell instances cannot access each other’s shell variables.
To list all variables and their definitions visible to the current shell, use the set command:
1set
Since this command produces extensive output, it can be piped to head to show just the first few variable definitions:
1set | head -4
To define a new shell variable, use the equal sign to assign a value to a variable name. Note that there should be no spaces around the equal sign:
1GREETINGS=hello
To access the value of a shell variable, prefix the variable name with a dollar sign ($) and use it with commands like echo:
1echo $GREETINGS
Multiple variables can be used together in a single command:
1AUDIENCE=world
2echo $GREETINGS $AUDIENCE
This would output: hello world
To clear a variable, use the unset command:
1unset AUDIENCE
Environment variables are similar to shell variables but have extended scope. They persist in any child processes spawned by the shell in which they originate.
To extend a shell variable to an environment variable, use the export command:
1export GREETINGS
This makes the GREETINGS variable available to all child processes of the current shell.
To list all environment variables, use the env command:
1env
To check if a specific variable exists as an environment variable, pipe the output of env to grep with a pattern:
1env | grep GREE
This would confirm whether GREETINGS is available as an environment variable.
Filters and pipes are powerful features in Linux that allow for sophisticated data processing through command chaining. Shell variables provide a way to store and reuse values within a shell session, while environment variables extend this capability to child processes. Understanding these concepts is fundamental for effective shell scripting and command-line work in Linux environments.
wc, cat, more, head, sort, and grep.|), allows users to chain multiple filter commands together. It creates a pipeline where the output of one command becomes the input for the next command. The syntax is command1 | command2 | command3, where each command processes the output from the previous command in sequence.The key differences between shell variables and environment variables are:
VARIABLE=value)export VARIABLE)| Command | Description |
|---|---|
wc | Counts lines, words, and characters |
cat | Concatenates and displays file contents |
more | Displays output one screen at a time |
head | Displays the beginning of a file |
sort | Sorts input lines |
grep | Searches for patterns in text |
GREETINGS=hello. The variable name is case-sensitive and by convention, environment variables are often uppercase.$). For example, to display the value of a variable named GREETINGS, you would use: echo $GREETINGS. Multiple variables can be used together in a single command, such as echo $GREETINGS $AUDIENCE.False. Environment variables are only available to the shell in which they were created and any child processes spawned by that shell. They are not automatically available to all shell instances on the system or to parent processes.
export command is used to convert a shell variable to an environment variable. For example, after creating a shell variable with GREETINGS=hello, you would use export GREETINGS to make it available to all child processes of the current shell.env command is used to list all environment variables. This displays all variables that have been exported and are available to child processes. To search for specific environment variables, you can pipe the output to grep, as in env | grep PATTERN.A practical example of using pipes to filter directory contents would be:
1ls -l | grep "^d" | sort -k 9
This command:
ls -l)grep "^d" - lines starting with ’d')This pipeline efficiently finds and sorts directories, demonstrating how pipes can combine simple commands to perform more complex operations.
unset command followed by the variable name (without the $ prefix). For example, unset AUDIENCE would delete the variable named AUDIENCE from the shell’s environment.The command ls | wc -l will count and display the number of files and directories in the current directory. The ls command lists the directory contents, and its output is piped to wc -l, which counts the number of lines in that output. Each line represents one file or directory, so the final result is the total count of items in the directory.
When you create a shell variable with the same name as an existing environment variable, the new value overrides the old one in the current shell. However, the original environment variable remains unchanged in other shells or processes that were started before this change. If you export the variable again, child processes will receive the new value, but parent processes retain their original copy of the variable.
set command lists all variables and their definitions that are visible to the current shell, including both shell variables and environment variables. It provides a comprehensive view of all variables accessible in the current shell environment, which can be useful for debugging or understanding the shell’s state.The command head -3 example.txt will display only the first three lines of the file ’example.txt’. The head command by default shows the first 10 lines of a file, but the -3 option limits the output to exactly 3 lines.