I have a script that is going to be executed by sh and bash on Debian Linux.
- When it is called by
sh
, the following echo command works perfectly, and replaces\t
with 3 spaces. However, it fails when using#!/bin/bash
.
Output#/bin/sh echo "Hello\tworld"
Hello world
- When it is called by
bash
, the following echo command works perfectly, and replaces\t
with 3 spaces. However, it fails when run with#/bin/sh
.
Output#/bin/bash echo -e "Hello\tworld"
Hello world
Is there any way where the same line command to replace \t sh
or bash?
#!
-line will be ignored as they are faulty (does not start with#!
). Also note that neither command would ever output a single space!echo
is a built-in in both sh (which is usually a link to ksh) and in bash, and they are slightly differently specified. Using/bin/echo
should at least be consistent. In any case, neither sh nor bash expands the TAB into three spaces:od -t a
shows a Tab is output. Any expansion is done by the terminal emulator. If you echo into a file, it will be 12 bytes long (10 letters, one tab, one newline), not 14.\t
with anything. It is only that some interpret it and display it as a tab and others as a literal` and
t`, but none of them convert it to spaces. Can you explain what your final objective is here?sh
andbash
on Debian Linux" then it's ash
script and you should mark as such with the#!/bin/sh
and not call it withbash
echo
is like that, it behaves differently in different shells. Useprintf 'Hello\tworld\n'
if you want to output a tab and a newline. Like you get with Bash'secho -e
, and Dash'secho
, Dash being what your Debian very likely has as/bin/sh
. If you really, seriously, want to output a particular number of spaces from the script, then that's not something either of those commands do. For that, you may want to ask another question.