Sponsored Content
Operating Systems OS X (Apple) Help in explaining this echo conundrum. Post 302993798 by wisecracker on Tuesday 14th of March 2017 01:41:35 PM
Old 03-14-2017
Help in explaining this echo conundrum.

OSX 10.12.3, default bash terminal.
Consider this code and note it is calling 'sh' inside the code...
Code:
#!/bin/sh
echo '1\n2\n2\n3\n5' > /tmp/text
hexdump -C /tmp/text
/bin/echo '1\n2\n3\n4\n5' > /tmp/text
hexdump -C /tmp/text

Now view the interactive mode below, note the underlying shell is bash:-
Code:
Last login: Tue Mar 14 17:12:45 on ttys001
AMIGA:barrywalker~> # Use sh first...
AMIGA:barrywalker~> sh
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> echo '1\n2\n3\n4\n5' > /tmp/text
AMIGA:barrywalker~/Desktop/Code/Shell> hexdump -C /tmp/text
00000000  31 0a 32 0a 33 0a 34 0a  35 0a                    |1.2.3.4.5.|
0000000a
AMIGA:barrywalker~/Desktop/Code/Shell> /bin/echo '1\n2\n3\n4\n5' > /tmp/text
AMIGA:barrywalker~/Desktop/Code/Shell> hexdump -C /tmp/text
00000000  31 5c 6e 32 5c 6e 33 5c  6e 34 5c 6e 35 0a        |1\n2\n3\n4\n5.|
0000000e
AMIGA:barrywalker~/Desktop/Code/Shell> ./echo_test.sh
00000000  31 0a 32 0a 32 0a 33 0a  35 0a                    |1.2.2.3.5.|
0000000a
00000000  31 5c 6e 32 5c 6e 33 5c  6e 34 5c 6e 35 0a        |1\n2\n3\n4\n5.|
0000000e
AMIGA:barrywalker~/Desktop/Code/Shell> # Now exit sh...
AMIGA:barrywalker~/Desktop/Code/Shell> exit
exit
AMIGA:barrywalker~> bash -version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> echo '1\n2\n3\n4\n5' > /tmp/text
AMIGA:barrywalker~/Desktop/Code/Shell> hexdump -C /tmp/text
00000000  31 5c 6e 32 5c 6e 33 5c  6e 34 5c 6e 35 0a        |1\n2\n3\n4\n5.|
0000000e
AMIGA:barrywalker~/Desktop/Code/Shell> /bin/echo '1\n2\n3\n4\n5' > /tmp/text
AMIGA:barrywalker~/Desktop/Code/Shell> hexdump -C /tmp/text
00000000  31 5c 6e 32 5c 6e 33 5c  6e 34 5c 6e 35 0a        |1\n2\n3\n4\n5.|
0000000e
AMIGA:barrywalker~/Desktop/Code/Shell> ./echo_test.sh
00000000  31 0a 32 0a 32 0a 33 0a  35 0a                    |1.2.2.3.5.|
0000000a
00000000  31 5c 6e 32 5c 6e 33 5c  6e 34 5c 6e 35 0a        |1\n2\n3\n4\n5.|
0000000e
AMIGA:barrywalker~/Desktop/Code/Shell> _

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.

TIA...
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

explaining awk

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)
Discussion started by: phone_book
1 Replies

2. UNIX for Dummies Questions & Answers

Need help explaining how to use a VPN on a UNIX server with a Mac OS

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)
Discussion started by: psycopuppy
0 Replies

3. UNIX for Dummies Questions & Answers

Explaining some lines from files : .login and .cshrc

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)
Discussion started by: bbqtoss
5 Replies

4. UNIX for Dummies Questions & Answers

umask conundrum

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)
Discussion started by: Irishboy24
1 Replies

5. Shell Programming and Scripting

Conundrum - Flexible way to strip extension

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)
Discussion started by: Spadez
3 Replies

6. Shell Programming and Scripting

sudo scripts conundrum

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)
Discussion started by: delphys
3 Replies

7. Shell Programming and Scripting

Help to explaining a command in run dot tcl

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)
Discussion started by: mar85
6 Replies

8. IP Networking

iptables conundrum

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)
Discussion started by: Shocco
3 Replies
CPANPLUS::Shell::Default::Plugins::Source(3)		User Contributed Perl Documentation	      CPANPLUS::Shell::Default::Plugins::Source(3)

NAME
CPANPLUS::Shell::Default::Plugins::Source - read in CPANPLUS commands SYNOPSIS
CPAN Terminal> /source /tmp/list_of_commands /tmp/more_commands DESCRIPTION
This is a "CPANPLUS::Shell::Default" plugin that works just like your unix shells source(1) command; it reads in a file that has commands in it to execute, and then executes them. A sample file might look like this: # first, update all the source files x --update_source # find all of my modules that are on the CPAN # test them, and store the error log a ^KANE$' t * p /home/kane/cpan-autotest/log # and inform us we're good to go ! print "Autotest complete, log stored; please enter your commands!" Note how empty lines, and lines starting with a '#' are being skipped in the execution. BUG REPORTS
Please report bugs or other issues to <bug-cpanplus@rt.cpan.org<gt>. AUTHOR
This module by Jos Boumans <kane@cpan.org>. COPYRIGHT
The CPAN++ interface (of which this module is a part of) is copyright (c) 2001 - 2007, Jos Boumans <kane@cpan.org>. All rights reserved. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. SEE ALSO
CPANPLUS::Shell::Default, CPANPLUS::Shell, cpanp perl v5.16.3 2013-05-20 CPANPLUS::Shell::Default::Plugins::Source(3)
All times are GMT -4. The time now is 07:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy