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! – Kusalananda Jul 18 '21 at 16:59echo
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. – Paul_Pedant Jul 18 '21 at 17:24sh
a link toksh
? I've never come across that. It's a link tobash
on many systems, sincebash
runs in compatibility mode, and it is a symlink todash
on Ubuntu and Debian and their derivatives. I don't know of any Linux that has it pointing to ksh, is that common in the UNIX world perhaps? – terdon Jul 18 '21 at 18:18\t
with anything. It is only that some interpret it and display it as a tab and others as a literal\
andt
, but none of them convert it to spaces. Can you explain what your final objective is here? – terdon Jul 18 '21 at 18:21sh
andbash
on Debian Linux" then it's ash
script and you should mark as such with the#!/bin/sh
and not call it withbash
– Chris Davies Jul 18 '21 at 18:24sh
isksh
on OpenBSD, but that's beside the point as the OP says they are on Debian GNU/Linux. – Kusalananda Jul 18 '21 at 18:26echo
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. – ilkkachu Jul 18 '21 at 18:54/bin/echo
would work the same on that particular system regardless of the shell they used, but you'd still get different results if you happened to move to another system where this time/bin/echo
does things differently. Let's just, you know, not useecho
at all if compatibility is an issue. – ilkkachu Jul 18 '21 at 18:59