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 ------------------------------