简体   繁体   中英

How can I print a newline from within a bash function

I have the following piece of code inside a bash script file:

#! /bin/sh
#
# Usage:
#
# f.sh <start_directory> <destination_directory> <prepare_directory> <remote_host_for_copy> <remote_user>
#
print_correct_syntax() {

 echo "f.sh <start_directory> <destination_directory> <prepare_directory> <remote_host_for_copy> <remote_user>"
 echo ""
 echo '<start_directory> needs to be a full path, <destination_directory> needs to be full path, <prepare_directory> has to be relative to start_directory path'
 echo ""

}
# ---- end of function definition ----

# check number of run-time arguments given to script

if [ $# != 5 ]
then
 echo Wrong Syntax! $(print_correct_syntax)
 exit;
fi
#---------------------------------------------------

The echo empty string commands (echo "") inside the function (print_correct_syntax) do not seem to be working. On the contrary, same commands outside function seem to be working fine. How can I print an empty newline from within a bash function?

What you put in backquotes (or $() ) gets the newlines translated into spaces. You can directly call the function instead of using it inside backquotes.

In other words, the function actually generates the empty lines, but the backquoting process with $() translates them into spaces when you use it in your final echo .

Try to replace:

echo Wrong Syntax! $(print_correct_syntax)

by:

echo Wrong Syntax! "$(print_correct_syntax)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM