Browse Courses

Filter Pipes Variables

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 and Pipes in Linux

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.

Common Filter Commands

Several commands in Linux operate as filters, including:

CommandDescription
wcCounts lines, words, and characters
catConcatenates and displays file contents
moreDisplays output one screen at a time
headDisplays the beginning of a file
sortSorts input lines
grepSearches for patterns in text

Pipe Command

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:

  • Output from command1 becomes input for command2
  • Output from command2 becomes input for command3
  • And so on for any number of commands

Example of Pipe Usage

1ls | 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

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.

Listing 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

Creating Shell Variables

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

Accessing Shell Variables

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

Removing Shell Variables

To clear a variable, use the unset command:

1unset AUDIENCE

Environment Variables

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.

Converting Shell Variables to Environment Variables

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.

Listing Environment Variables

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.


Conclusion

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.


FAQs

Filters are shell commands or programs that take input from standard input (typically the keyboard) and send output to standard output (typically the terminal). They function as transformers that convert input data into a different form of output data. Common examples include wc, cat, more, head, sort, and grep.

The pipe command, represented by the vertical bar symbol (|), 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:

  • Shell variables are limited in scope to the shell in which they were created; different shell instances cannot access each other’s shell variables
  • Environment variables have extended scope and persist in any child processes spawned by the shell in which they originate
  • Shell variables are created with simple assignment (VARIABLE=value)
  • Environment variables are created by exporting shell variables (export VARIABLE)

Click to see answer
CommandDescription
wcCounts lines, words, and characters
catConcatenates and displays file contents
moreDisplays output one screen at a time
headDisplays the beginning of a file
sortSorts input lines
grepSearches for patterns in text

To create a shell variable in Linux, use the equal sign to assign a value to a variable name, with no spaces around the equal sign. For example: GREETINGS=hello. The variable name is case-sensitive and by convention, environment variables are often uppercase.

To access the value of a shell variable, prefix the variable name with a dollar sign ($). 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.

Click to see answer

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.

The 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.

The 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.

Click to see answer

A practical example of using pipes to filter directory contents would be:

1ls -l | grep "^d" | sort -k 9

This command:

  1. Lists all files and directories in long format (ls -l)
  2. Filters to show only directories (grep "^d" - lines starting with ’d')
  3. Sorts the results alphabetically by name (column 9 in the output)

This pipeline efficiently finds and sorts directories, demonstrating how pipes can combine simple commands to perform more complex operations.

To remove or clear a shell variable, use the 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.

Click to see answer

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.

Click to see answer

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.

The 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.

Click to see answer

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.