LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Giving wrong output with sh but correct output with command entry in Ubuntu (https://www.linuxquestions.org/questions/linux-newbie-8/giving-wrong-output-with-sh-but-correct-output-with-command-entry-in-ubuntu-4175556186/)

bison72 10-14-2015 09:46 PM

Giving wrong output with sh but correct output with command entry in Ubuntu
 
Hi there,

I want to execute the command as below using sh:
printf '"NA" [slash]"\\t"[modulus].0s' {1..4}

PS: Sorry I have to put a "modulus" and "slash" up there because the symbols are missing in the message.

which will give output as below that I want:
"NA" "\t""NA" "\t""NA" "\t""NA" "\t"

I could execute the file using sh in Fedora. However, when I run sh in Ubuntu, it shows the incorrect output below:
"NA" [slash]"\t[slash]"

When I execute the command, it could give exactly what I want.

This happens in Ubuntu but not in Fedora. Anyone has any idea how to fix this?

Thanks in advance.

chrism01 10-14-2015 10:16 PM

I think on Ubuntu sh is symlinked (or replaced ?) with dash.
On my Centos, which should probably be the same as Fedora, both 'sh' and 'bash' work, although I did have to add one more '\'
Code:

printf "\"NA\" \"\\\t\"%.0s" {1..4}

bash t1.sh
"NA" "\t""NA" "\t""NA" "\t""NA" "\t"

sh t1.sh
"NA" "\t""NA" "\t""NA" "\t""NA" "\t"[chris@boole lq]$

dash t1.sh
"NA" "\t"

HTH

bison72 10-14-2015 10:29 PM

Hi Chris,

Appreciate your help. It works with bash but not with sh or dash. I wrote the script in Fedora and this is the first time I execute it in Ubuntu. :)

Rgds,
Louis

serverpoint.com 10-14-2015 11:27 PM

Hi Louis,

Can you confirm this by running the command below. What is the output?

ls -la /bin/sh


All times are GMT -5. The time now is 07:46 PM.