OSX 10.12.3, default bash terminal.
Consider this code and note it is calling 'sh' inside the code...
Now view the interactive mode below, note the underlying shell is bash:-
1) Why does the 'bash' interactive mode give a different result to the 'sh' mode as 'sh' is merely a subset of 'bash'?
2) Why does echo in 'sh' translate '\n' to newlines in single quotes?
I thought single quotes were to give a raw string of characters, irrespective?
(Is this an Apple OSX thing?)
The 'sh' version is the same as the 'bash' version but are NOT identical files.
'echo' is not consistent or portable between different shells. Sometimes you get newlines, sometimes you get \n, sometimes you only get newlines when you ask for them with echo -e, and sometimes you can't get newlines out no matter what you do (i.e. /bin/sh on solaris). Given OSX's nextstep/mach lineage, I'll wild-guess that their /bin/sh is meant to resemble an old Bourne from BSD.
Note that there is a reason that echo is not part of the POSIX specification. For consistent results, it is best to only use printf
I get
So in the first case \n is interpreted as newline, in the second case it is not.
--
Interestingly:
When I do: /bin/bash --posix, which is supposed to be the same as /bin/sh I get:
Also when bash is invoked without the --posix option
On OSX, apparently it is not a link to /bin/bash as is the case on some Linux distributions, but rather they are two distinct binaries
If I make a symlink sh -> /bin/bash
Last edited by Scrutinizer; 03-14-2017 at 03:08 PM..
This User Gave Thanks to Scrutinizer For This Post:
Also interestingly when using 'dash' on OSX 10.12.3 default bash terminal:-
Seems consistent with 'sh'.
I am going to have to be careful with this.
---------- Post updated at 07:39 PM ---------- Previous update was at 07:10 PM ----------
Quote:
Originally Posted by Corona688
'echo' is not consistent or portable between different shells. Sometimes you get newlines, sometimes you get \n, sometimes you only get newlines when you ask for them with echo -e, and sometimes you can't get newlines out no matter what you do (i.e. /bin/sh on solaris). Given OSX's nextstep/mach lineage, I'll wild-guess that their /bin/sh is meant to resemble an old Bourne from BSD.
printf is a lot more consistent.
I do know a few ideas but do not know if this would work in the Solaris situation you quoted.
This is longhand using 'dash' and '/bin/echo'...
You will have to take my word for the 'bold' as it does not copy and paste too well... ;o)
There are ways to bend rules even in dash...
Last edited by wisecracker; 03-14-2017 at 04:43 PM..
Reason: Sorry about the attachment. I did expect a new window.
Seems consistent with 'sh'.
I am going to have to be careful with this.
Basically, only use echo if you want to print preformatted / unescaped data: Any extended behavior like processing backslashes isn't portable.
Quote:
I do know a few ideas but do not know if this would work in the Solaris situation you quoted.
I don't see any point fighting it when there's better alternatives though. Your solution is twice as complicated and much less efficient than just using printf once to do both jobs. printf "\033[1mBold Text\033[0m Normal Text\n"
You're not breaking the rules, though It's completely fine to store any ASCII character besides NULL in a variable.
Last edited by Corona688; 03-15-2017 at 12:45 PM..
I don't see any point fighting it when there's better alternatives though. Your solution is twice as complicated and much less efficient than just using printf once to do both jobs.
Perhaps but my non-rooted Android phone does NOT have printf it only has the same as my echo but the terminal I have on it does emulate the escape codes and the shell resembles a cut down version of bash, probably 'bash --posix' as I can only call it as 'sh'. Hence my experiments with 'echo' over a period of time.
Ok, if youre reading this prepare yourself.(debian based os)
so im trying to do this routing with ip tables, i need to forward/SNAT traffic from 192.168.111.1 to 10.10.10.250, the 192.x.x.x ips are being shoved into a honeyd like program called inetsim so its offline, 10.10.10.125 is connected... (3 Replies)
Hi,
I'm running a rdt (run dot tcl) command, and come accross this line:
alias abc 'set ARGS =(\!*); source home123/abc/$ARGS/setup'
What does the command exactly do?
Please help. (6 Replies)
hello; Got a problem running monitoring scripts using sudo ssh.. Mgmt decided to take away root sudoers access.. so most of the scripts ran as:
sudo ssh $BOX ...
Now I need to run them as:
echo $my_pw | sudo -S -l my_user_id $BOX ...
I tried this but not working..
Any wisdom/tricks... (3 Replies)
Hi,
First post here. I have something that may prove to be difficult.
I have the following files:
Example1.0.0.tar.gz
Example2.tar
Example3.zip
Example4.0.0.0.0.0.bzip2
I need to remove the file extensions and store as a variable so they look like this:
Example1.0.0
Example2... (3 Replies)
Hi All,
i was reading up on a umask question on this forum and have a question on this.
the umask value on my home PC running on cygwin is 022. when i create a dir it defaults to permission 755, when i create a file it defaults to 644. Now it starts at 777 for dirs and 666 for files and... (1 Reply)
Hello, can anyone explain me please what do those lines do ?
From file .login
1) set history=40
2) setenv MACH `uname -s`
3) source /etc/login
4) source ~/$MACH/.login
From file .cshrc
1) if ( ! $?prompt) exit 0 (5 Replies)
I have gotten a gig to teach someone how to use a VPN client for a UNIX server on a MAC os. The problem is I have never used UNIX, dont mess with VPN's (my dad has a VPN that I have used a couple of times). I'm currently taking a crash course on UNIX but I was wondering if anyone could help me with... (0 Replies)
i am new to awk scripting. i couldn't figure out how this awk script works can anyone explain?
#!/bin/awk -f
{
for( x=1; x<=NF; ++x) {
if( x == 3 ) {
continue
}
print x, $x
}
}
thank you and regards (1 Reply)