How to put newlines or returns in a shell variable *and* check they're there?
I want to set a variable in a shell (bash) script with a newline as one of the characters. First question, how do you do it? Second question, how do you prove it's there once you've done it? I *think* there are several fairly obvious ways of doing it, e.g. things like:- export fred="abcde ghijk" However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all. If you do the above and then say "echo $fred" you just get:- abcde ghijk Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables). -- Chris Green
On 3 Apr 2007, at 1:43 pm, Chris G wrote:
I want to set a variable in a shell (bash) script with a newline as one of the characters.
First question, how do you do it? Second question, how do you prove it's there once you've done it?
I *think* there are several fairly obvious ways of doing it, e.g. things like:-
export fred="abcde ghijk"
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
abcde ghijk
Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables).
A small amount of googling[1] finds this... export test="bunch of \n text" echo $test bunch of \n text echo -e $test bunch of text I think this is based on bash, so may be different in other shells. Hope that helps, David [1] Go to http://www.google.co.uk and type in 'bash newline', hit the search button and marvel at the results. -- David Reynolds david@reynoldsfamily.org.uk
On Tue, Apr 03, 2007 at 02:12:57PM +0100, David Reynolds wrote:
On 3 Apr 2007, at 1:43 pm, Chris G wrote:
I want to set a variable in a shell (bash) script with a newline as one of the characters.
First question, how do you do it? Second question, how do you prove it's there once you've done it?
I *think* there are several fairly obvious ways of doing it, e.g. things like:-
export fred="abcde ghijk"
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
abcde ghijk
Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables).
A small amount of googling[1] finds this...
export test="bunch of \n text" echo $test bunch of \n text
echo -e $test bunch of text
Been there, done that, doesn't work for me! :-) chrisg$ export fred="abcde \n fghij" chrisg$ echo "$fred" abcde \n fghij chrisg$ This is bash on FC6. -- Chris Green
On 03-Apr-07 14:14:34, Chris G wrote:
On Tue, Apr 03, 2007 at 02:12:57PM +0100, David Reynolds wrote:
On 3 Apr 2007, at 1:43 pm, Chris G wrote:
I want to set a variable in a shell (bash) script with a newline as one of the characters.
First question, how do you do it? Second question, how do you prove it's there once you've done it?
I *think* there are several fairly obvious ways of doing it, e.g. things like:-
export fred="abcde ghijk"
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
abcde ghijk
Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables).
A small amount of googling[1] finds this...
export test="bunch of \n text" echo $test bunch of \n text
echo -e $test bunch of text
Been there, done that, doesn't work for me! :-)
chrisg$ export fred="abcde \n fghij" chrisg$ echo "$fred" abcde \n fghij chrisg$
This is bash on FC6.
And it won't do what you want anyway -- the string contents of $fred will be "abcde \n fghij" (each character literally, apart from the "" of course). What "echo -e" does is translate the "\n" into a newline when $fred ia read. There is no newline in $fred itself. "-e enable interpretation of the backslash-escaped characters" It looks as though MJR got the answer! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Apr-07 Time: 15:28:47 ------------------------------ XFMail ------------------------------
On 3 Apr 2007, at 3:14 pm, Chris G wrote:
On Tue, Apr 03, 2007 at 02:12:57PM +0100, David Reynolds wrote:
On 3 Apr 2007, at 1:43 pm, Chris G wrote:
I want to set a variable in a shell (bash) script with a newline as one of the characters.
First question, how do you do it? Second question, how do you prove it's there once you've done it?
I *think* there are several fairly obvious ways of doing it, e.g. things like:-
export fred="abcde ghijk"
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
abcde ghijk
Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables).
A small amount of googling[1] finds this...
export test="bunch of \n text" echo $test bunch of \n text
echo -e $test bunch of text
Been there, done that, doesn't work for me! :-)
chrisg$ export fred="abcde \n fghij" chrisg$ echo "$fred" abcde \n fghij chrisg$
You missed the '-e' after the echo, but as Ted says, it looks like MJ got it. -- David Reynolds david@reynoldsfamily.org.uk
On Tue, Apr 03, 2007 at 03:32:20PM +0100, David Reynolds wrote:
On 3 Apr 2007, at 3:14 pm, Chris G wrote:
On Tue, Apr 03, 2007 at 02:12:57PM +0100, David Reynolds wrote:
On 3 Apr 2007, at 1:43 pm, Chris G wrote:
Been there, done that, doesn't work for me! :-)
chrisg$ export fred="abcde \n fghij" chrisg$ echo "$fred" abcde \n fghij chrisg$
You missed the '-e' after the echo, but as Ted says, it looks like MJ got it.
Ah, oops, so I did. So now it's working for me too. Thanks all. I think my problem was reading the man page for echo which says:- -e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default) as that doesn't suggest to me anything like what it actually does! I think what it's actually doing is echoing the characters as they *really* are when you give it -e whereas with -E it turns any white space into a single space. -- Chris Green
On Tue, Apr 03, 2007 at 03:45:30PM +0100, Chris G wrote:
You missed the '-e' after the echo, but as Ted says, it looks like MJ got it.
Ah, oops, so I did. So now it's working for me too. Thanks all.
I think my problem was reading the man page for echo which says:-
-e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default)
as that doesn't suggest to me anything like what it actually does! I think what it's actually doing is echoing the characters as they *really* are when you give it -e whereas with -E it turns any white space into a single space.
No, no, no, no, no - oh dear, I've confused myself haven't I. The echo man page is quite correct and (as others have noted) all that's happening above is that echo is turning the \n into a newline. However I now think I can get what I want and show it:- chrisg$ fred="abcde"$'\n'"ghijk" chrisg$ echo $fred abcde ghijk chrisg$ The sequence $'\n' in bash is a string containing a single newline character (most of the other familiar \ specials work too). -- Chris Green
On 03-Apr-07 14:52:07, Chris G wrote:
No, no, no, no, no - oh dear, I've confused myself haven't I. The echo man page is quite correct and (as others have noted) all that's happening above is that echo is turning the \n into a newline.
However I now think I can get what I want and show it:-
chrisg$ fred="abcde"$'\n'"ghijk" chrisg$ echo $fred abcde ghijk chrisg$
The sequence $'\n' in bash is a string containing a single newline character (most of the other familiar \ specials work too).
Hmmm ... Trying the above: fred="abcde"$'\n'"ghijk" echo $fred abcde ghijk echo "$fred" abcde ghijk I wonder why ... ?? At least, the $'\n' is a smooth way to get the newline into $fred! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Apr-07 Time: 16:10:44 ------------------------------ XFMail ------------------------------
Chris G <cl@isbd.net> wrote:
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
That's bash doing the "clever" thing not echo. The command you want to use to check is: echo "$fred" ...which stops bash fiddling with the whitespace before passing it to echo (same happens with both the builtin and /bin/echo). Hope that helps, -- MJ Ray - see/vidu http://mjr.towers.org.uk/email.html Webmaster/web developer, statistician, sysadmin, online shop maker, developer of koha, debian, gobo, gnustep, various mail and web s/w. Workers co-op @ Weston-super-Mare, Somerset http://www.ttllp.co.uk/
On 03-Apr-07 13:17:42, MJ Ray wrote:
Chris G <cl@isbd.net> wrote:
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
That's bash doing the "clever" thing not echo. The command you want to use to check is: echo "$fred" ...which stops bash fiddling with the whitespace before passing it to echo (same happens with both the builtin and /bin/echo).
Hope that helps, -- MJ Ray - see/vidu http://mjr.towers.org.uk/email.html
Yep!!! fred="abcde
ghijk"
echo "$fred" abcde ghijk ... despite my previous mail (which didn't think of the quoting at all)! Nice one. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Apr-07 Time: 14:36:05 ------------------------------ XFMail ------------------------------
On 03-Apr-07 12:43:04, Chris G wrote:
I want to set a variable in a shell (bash) script with a newline as one of the characters.
First question, how do you do it? Second question, how do you prove it's there once you've done it?
I *think* there are several fairly obvious ways of doing it, e.g. things like:-
export fred="abcde ghijk"
However the problem is that using 'echo' to see what you have done is totally useless as echo does so many clever things under the cover that you don't get to see what's actually in the variable at all.
If you do the above and then say "echo $fred" you just get:-
abcde ghijk
Harrumph! *Surely* there's some way of saying "show me what's really in this variable/string" (a bit like 'od' but for variables).
I think it's not going in as you expected, Chris. There's a check you can do with 'tr -d': export fred="abcde ghijk" echo $fred | tr -d '\n' abcde ghijk echo $fred | tr -d ' ' abcdeghijk If $fred had a newline in it, then "tr -d '\n'" would have deleted it. On the other hand, "tr -d ' '" will delete a SPACE character (and nothing else); and this is what it did! So your apparent (on input) newline in fact went in as a space. Interestingly, if I try the "tr" trick to force the newline on input: export fred=`echo "abcdeXghijk" | tr 'X' '\n'` I get exactly the same results as above. So this doesn't do the trick either. Indeed: echo $fred | od -x 0000000 6261 6463 2065 6867 6a69 0a6b and the "20" shows it's a real space caracter. A further test: fred="" fred="ls ls" $fred ls: ls: No such file or directory so $fred is trying to execute ls ls and not ls ls So I think that, unless some way really does exist of getting a newline into an envar, it's always going to be a space. Oh dear. Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding@manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-Apr-07 Time: 14:30:41 ------------------------------ XFMail ------------------------------
participants (4)
-
Chris G -
David Reynolds -
MJ Ray -
ted.harding@nessie.mcc.ac.uk