Sample Header Ad - 728x90

Bash parameter substitution with multi line error message

1 vote
1 answer
400 views
I'm using parameter substitution with an error message, such as
${var1:?'some message'}
. I've incorporated a multi-line error message. Presently, it only functions properly when enclosed within single quotes, with line breaks inserted using the Enter key. Is there a clever way to allow acceptance of multiple lines, saved separately? I'm just curious about exploring this. Please avoid suggesting alternative syntax involving if statements unless it's directly relevant. **Cases:** works: single quote
needed=${1:?'first error line
second error line
third error line'}
not working: call another string variable
usage_message='Please use this script by providing the following arguments:
1: 
2: 
3: '

username=${1:?$usage_message}
run_script_path=${2:?$usage_message}
where_to_save=${3:?$usage_message}
not working: call a function
function get_message {
       echo -e "first line \nsecond line\nthird line"
       # or
       # printf "first line \nsecond line\nthird line"
}

needed=${1:? $(get_message)}
Other discussion about parameter substitution: https://stackoverflow.com/a/77772942/13413319
Asked by programmer Erfan (13 rep)
Feb 20, 2024, 01:37 AM
Last activity: Feb 20, 2024, 03:25 PM