Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

scripting - How to debug a bash function that returns a value, and how to add newlines to a variable?

I'm in a bash crash course today.

This is a bash function that returns a value, via echo:

#!/bin/bash
get_hello_name() {
  echo 'Hello $1!'
}
msg=$(get_hello_name "x")
echo $msg

Output:

$ bash ./initial_script5.sh
Hello $1!

I then incorrectly thought that the last echo was returned (I come from Java and Python), and was trying to use echo to debug the rest of the function.

And then I was wondering why the heck I could not print out newlines in my echo statements, despite trying every single suggestion in this question.

This script demonstrates the problem:

#!/bin/bash
a_function() {
  echo "---In function"

  printf %"s
" hello world
  printf "Hello
world"
  echo $'hello
world'
  echo -e 'hello
world'
}

echo "---Pre function"
printf %"s
" hello world
printf "Hello
world"
echo $'hello
world'
echo -e 'hello
world'

x=$(a_function "x")
echo $x

echo "---Post function"
printf %"s
" hello world
printf "Hello
world"
echo $'hello
world'
echo -e 'hello
world'


$ bash ./initial_script5.sh
---Pre function
hello
world
Hello
worldhello
world
hello
world
---In function hello world Hello worldhello world hello world
---Post function
hello
world
Hello
worldhello
world
hello
world

The problem is that all of the echos in the function are concatenated together, after being individually trimmed, and then returned as a whole.

So this leads me to two questions: How do you debug a function that returns a value, and how do you append newlines onto a variable (not that I even want to do the latter, necessarily, but I would like to understand it)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why the newlines seemed to disappear from the variable

The newlines are actually retained in the variable. They do not display because the variable in the echo statement is not enclosed in double-quotes. From the code:

echo $x

When using a variable without double-quotes, word splitting is performed. Under the default $IFS (wiki entry on IFS), this means that all collections of whitespace, including newlines and tabs, are replaced with single space.

To avoid that, simply use double quotes as in:

echo "$x"

With that single change, the output of your script becomes:

$ bash a,sh 
---Pre function
hello
world
Hello
worldhello
world
hello
world
---In function
hello
world
Hello
worldhello
world
hello
world
---Post function
hello
world
Hello
worldhello
world
hello
world

The newlines that were always in the variable x are now displayed.

Aside: the two words that remain strung together

Note that the combination worldhello appears on one line because because that is what the code asked for:

printf "Hello
world"
echo $'hello
world'

The printf does not print a newline after world. Hence, that world appears on the same line as the hello which follows.

Documentation of the Details

man bash explains that double-quotes inhibit word splitting:

If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.

Word-splitting happens after variable expansion, command substitution, and arithmetic expansion:

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

Another subtlety is that word splitting is performed only if some substitution took place:

Note that if no expansion occurs, no splitting is performed.

Normally, when word splitting is performed, all strings of spaces, tabs, and newlines are replaced by a single space. This default behavior can be changed by changing the value of the IFS variable:

The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words on these characters. If IFS is unset, or its value is exactly , the default, then sequences of space, tab, and newline at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

How to Debug

  1. Use set -x

    Place the line set -x at the beginning of the code that you wish to run. The results of evaluating each line will be displayed as the function is run, each preceded by PS4 (default is +, space) to distinguish it from normal output.

    The debug output can be turned off by including the line set +x.

    set -x and set +x both also work on the command line.

  2. Use stderr

    Send debug output to stderr (file descriptor 2) as follows:

    echo "My Debug Info" >&2
    

    By default, pipelines and command substitutions only operate on stderr. Consequently, information sent to stderr will, by default, appear on the terminal.

More on echo

By default, echo ignores escape characters and the sequence simply means a followed by an n:

$ echo "Hello
world 4"
Hello
world 4

To have interpreted as a newline, use -e:

$ echo -e "Hello
world 4"
Hello
world 4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...