Echo tab characters in bash script

Echo Tab Characters In Bash Script

Echo tab characters in bash script

echo -e  t 

will echo area tab area newline (-e means allow interpretation of backslash escapes):

$ echo -e  t  | hexdump -C
00000000  20 09 20 0a                                       | . .|

Use printf, not echo.

There are a number of completely different variations of the echo command. Theres /bin/echo (which can or might not be the GNU Coreutils model, relying on the system), and the echo command is constructed into most shells. Different variations have alternative ways (or no approach) to specify or disable escapes for management characters.

printf, then again, has a lot much less variation. It can exist as a command, sometimes /bin/printf, and its constructed into some shells (bash and zsh have it, tcsh and ksh dont), however the numerous variations are way more related to one another than the completely different variations of echo are. And you dont have to recollect command-line choices (with a number of exceptions; GNU Coreutils printf accepts --version and --help, and the built-in bash printf accepts -v var to retailer the output in a variable).

For your instance:

res=           x # res = ttx
printf %sn [$res]

And now its time for me to confess that echo will work simply as effectively for the instance youre asking about; you simply must put double quotes across the argument:

echo [$res]

as kmkaplan wrote (two and a half years in the past, I simply observed!). The downside together with your authentic instructions:

res=           x # res = ttx
echo [$res] # anticipate [ttx]

isnt with echo; its that the shell changed the tab with an area earlier than echo ever noticed it.

READ :  maven – SonarQube not picking up Unit Test Coverage

echo is okay for easy output, like echo hiya world, however you need to use printf everytime you need to do one thing extra complicated. You can get echo to work, however the ensuing code is more likely to fail while you run it with a special echo implementation or a special shell.

Echo tab characters in bash script

You may strive:

echo Hello$tworld.

Related posts on Bash Script :

Leave a Reply

Your email address will not be published. Required fields are marked *