output="$(awk -F',' '/Name/ {print $9}' input.file)"
How do trim leading and trailing whitespace from bash variable called $output? How do I trim trailing whitespace from $output?
You can use sed, awk, cut, tr, and other utilities to remove whitespace from $output.
Sample data
Define a variable called ouput:
output=" This is a test"
Use echo statement to display $output:
echo "=${output}="
Sample outputs:
= This is a test=
sed Example
The syntax is:
echo "${output}" | sed -e 's/^[ \t]*//'
Sample outputs:
This is a test
Bash Example
The syntax is to remove leading whitespaces:
${var##*( )}For example:
# Just remove leading whiltespace #turn it on shopt -s extglob output=" This is a test" output="${output##*( )}" echo "=${output}=" # turn it off shopt -u extglob
Sample outputs:
=This is a test=
To trim leading and trailing whitespace using bash, try:
#turn it on shopt -s extglob output=" This is a test " ### Trim leading whitespaces ### output="${output##*( )}" ### trim trailing whitespaces ## output="${output%%*( )} echo "=${output}=" # turn it off shopt -u extglob
Sample outputs:
=This is a test=
awk Example
The syntax is:
output=" This is a test " echo "=${output}=" ## Use awk to trim leading and trailing whitespace echo "${output}" | awk '{gsub(/^ +| +$/,"")} {print "=" $0 "="}'
Sample outputs:
=This is a test=
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 7 comments… read them below or add one }
check the title it says “While” Spaces
Thanks for the heads up!
If the content is a single line of text, why not just use echo ?
notice there are no double-quotes for the inner $VAR
… slight error …. it should be
This is the BEST site in the world. I can’t tell you how many times I have wound up here to find the answer to my question or problem. Great resource, thanks!!!
Hello..
Could you please help me out….
Here is my scenario:
1. i am listing some of the processes which are active and sending the output to temp.txt.
2. i got many processes, if i want to kill/doing some thing with any inactive process it should throw an error message.
3. how do i work with pattern matching commands in temp.txt?
Please suggest me the better answer..
m=$( echo "${output}" | sed -e "s/^\ *//g" -e "s/\ *$//g") echo "=${m}=" =This is a test=