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\twith 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\twith 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:59echois a built-in in both sh (which is usually a link to ksh) and in bash, and they are slightly differently specified. Using/bin/echoshould at least be consistent. In any case, neither sh nor bash expands the TAB into three spaces:od -t ashows 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:24sha link toksh? I've never come across that. It's a link tobashon many systems, sincebashruns in compatibility mode, and it is a symlink todashon 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\twith 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:21shandbashon Debian Linux" then it's ashscript and you should mark as such with the#!/bin/shand not call it withbash– Chris Davies Jul 18 '21 at 18:24shiskshon OpenBSD, but that's beside the point as the OP says they are on Debian GNU/Linux. – Kusalananda Jul 18 '21 at 18:26echois 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/echowould 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/echodoes things differently. Let's just, you know, not useechoat all if compatibility is an issue. – ilkkachu Jul 18 '21 at 18:59