• Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us

How to avoid whitespaces on a variable output in shell

Written by
Linux Leave a Comment

When you are scripting in shell , you might need the output of a shell command assigned to a variable and then you use that variable somewhere else in the script . So in this case there are chances that the whitespace occurs in the command output and when you use it somewhere else you will start seeing errors . So its very important to avoid white spaces in your scripts .

Fisrt echo the output in a curly braces and that way you will be able to see if there are whitespaces

So in the output below, I am printing the number of vpn clients connected in curly braces , you can see the white space before 36

root@freebsd:~ # /bin/sh test.sh
Current:  { 36}

So to avoid this use xargs at the end of your command, so that will be | xargs echo -n

command | xargs echo -n
root@freebsd:~ # /bin/sh test.sh
Current:  {36}

© Copyright 2020.TechieNix. All Rights Reserved.