Can I assign a string that ends with a multiple whitespace line in a bash shell variable? -
i got problem storing lines in bash shell variable.
there file contains string this.
$ vim a.txt ------we-are-in-vim------ first line second line -------end-of-file-------
this file has 2 empty lines placed on end of it.
when cat file, can see blank printed!
$ cat a.txt first line second line $
well. now, can imagine can put in bash variable.
let's try!
$ var=`cat a.txt` $ echo "${var}" first line second line $
ok. did not rap cat command's output double quotation! ;)
$ var="`cat a.txt`" $ echo "${var}" first line second line $
ok. let's try printf built-in variable assignment feature!
$ printf -v var "`cat a.txt`" $ echo "${var}" first line second line $
....ok let's try mapfile command!
$ mapfile < a.txt var $ printf '%s' "${var[@]}" first line second line $
the mapfile command worked, same cat!
$ var2=`printf '%s' "${var[@]}"` $ echo "${var2}" first line second line $
i have tried 'changing ifs nothing' in bash,
but result same!
how can assign string has 2 empty lines on end of it, bash variable?
it because how command-substitution in bash
works!
see man bash
except under command substitution
command substitution allows output of command replace command name. there 2 forms:
$(command)
`command`
[..] bash performs expansion executing command , replacing command substitution standard output of command with trailing newlines deleted. [..]
Comments
Post a Comment