Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:
Converting Output to String in Linux
Last updated: March 5, 2025
1. Overview
In Linux and shell scripting, a string is simply a sequence of characters. Manipulating and converting outputs into strings is important in performing command-line operations, shell scripting, and automation. Whether we’re working with a single command output or complex data streams, understanding how to convert the output to a string can help us process and handle data efficiently.
In this tutorial, we’ll explore how to convert output to string in Linux.
2. Using Command Substitution
Command substitution allows us to capture a command’s output and use it within another command or store it as a string in a variable. It’s useful when automating tasks, processing command output, or passing results from one command to another.
There are two syntaxes we can use to implement command substitution. One syntax is dollar parenthesis:
$ variable=$(command)
The other syntax is backticks:
$ variable=`command`
Here, we’ll use the dollar parenthesis $(command) syntax instead of backticks since it’s more readable and supports multi-line commands.
To begin, let’s find the current date and time:
$ current_date=$(date)
echo "The current date and time is: $current_date"
The current date and time is: 20 February 2025 07:25:06 asubuhi EAT
In this example, the date command generates the current date and time. $(date) captures this output and stores it in the current_date variable as a string. We then use echo to print a message that includes the value of the current_date variable.
Next, we can nest command substitutions to perform more complex tasks in a single statement:
$ user_info=$(echo "Current user: $(whoami), Logged-in users: $(who | wc -l)")
echo "$user_info"
Current user: samuel, Logged-in users: 1
This command displays the current user and the number of logged-in users. Using nested command substitution, we convert the output of whoami and who | wc -l into strings, and then store them in a variable named user_info.
Additionally, we can use command substitution in scripts to capture command outputs for logic decisions:
#!/bin/bash
# Capture disk usage of the root filesystem
disk_usage=$(df -h / | awk 'NR==2 {print $5}')
# Check if disk usage exceeds 80%
if [[ ${disk_usage%?} -ge 80 ]]; then
echo "Warning: Disk usage is high at $disk_usage"
else
echo "Disk usage is within limits: $disk_usage"
fi
The above script monitors disk usage of the root filesystem. df -h / | awk ‘NR==2 {print $5}’ captures the usage percentage and stores it in the disk_usage variable. We then use this value in a conditional statement to check if it exceeds 80%.
3. Using xargs
The xargs command transforms an input into a single-line string. It reads input from standard input and creates a command-line argument list. This is useful for converting multi-line output into a space-separated string. To explain, when a command produces output with multiple lines, xargs takes the output and joins it into a single line, separating each entry with a space.
To demonstrate, let’s list all the contents of the current directory as a single-line string:
$ ls -1 | xargs
config.json ideas.txt logs nginx.conf print server.conf settings.ini settings.yaml tests
Here, the ls -1 command lists each file and directory in the current directory on a new line. We then pipe the output of the ls -1 command as input in the xargs command, which converts the input into a single-line and space-separated string.
Additionally, using xargs, we can also take the output from one command and pass it as arguments to another command:
$ ls *.txt | xargs wc -w
13 ideas.txt
The above command counts the words in each file in the current directory. The ls *.txt command lists all files in the current directory with the .txt extension. Next, we pipe the output of ls *.txt to xargs, which takes the filenames and passes them as arguments to wc -w, which counts the number of words in each file.
4. Redirecting Output to a File
Redirecting output to a file and reading it back into a variable is another way to store output as a string. This approach is useful for handling large outputs or when we need to save results for later processing.
To redirect output to a file, we use the greater than > operator:
$ ls /etc > output.txt
output=$(<output.txt)
echo "$output"
Let’s understand the above command:
- ls /etc > output.txt – lists the contents of the /etc directory and redirects the output to a file named output.txt
- output=$(<output.txt) – reads the contents of output.txt into a variable named output
- echo “$output” – prints the string stored in the output variable
In this example, we capture the output of ls /etc and save it to a file. We then use command substitution to read that file into a variable, and then print the contents of the file. By default, the output keeps its original line breaks, meaning the string still contains multiple lines.
However, we can also convert the multi-line output into a single-line string:
$ ls /etc > output.txt
output=$(tr '\n' ' ' <output.txt)
echo "$output"
Above, we use the tr command to replace all newline characters in output.txt with spaces, converting the output into a single-line and space-separated string.
5. Conclusion
In this article, we explored how to convert output to a string in Linux. First, we discussed command substitution, which is best for capturing and using command output in variables or inline expressions. Next, we looked at xargs, which is useful for converting multi-line output into a single-line string and passing arguments to commands.
Lastly, we covered redirecting output to a file, which allows us to handle large outputs and store results for later processing.