Space runs when replacing one bash variable with another variable

I have problems saving spaces in a bash script:

CURRENT_YEAR=$(date "+%Y")
MESSAGE=$(eval echo "$(/usr/libexec/PlistBuddy -c "Print NSHumanReadableCopyright" Info.plist)")

/usr/libexec/PlistBuddy -c "Set NSHumanReadableCopyright $MESSAGE" ${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}

Corresponding section of Info.plist:

<NSHumanReadableCopyright>Copyright © BEC, ${CURRENT_YEAR}\nAll rights reserved.</NSHumanReadableCopyright>

The script reads the value from plist, then replaces another variable ( CURRENT_YEAR) with the value that was read from plist.

The problem is that the new line in MESSAGE is causing problems. If I use \n, instead of a new line, "n" is displayed. If the actual new line character is used, the command fails because "Everything" is interpreted as a new command.

How to get out of a new line? Is there a better way to do this? I have very little bash -foo.

Update - Solution:

Thanks for the help. The solution was to go crazy with quotes. Here's the end result:

CURRENT_YEAR="$(date "+%Y")"
MESSAGE_TEMPLATE="$(/usr/libexec/PlistBuddy -c "Print NSHumanReadableCopyright" Info.plist)"
MESSAGE=$(eval echo '"'"$MESSAGE_TEMPLATE"'"')
/usr/libexec/PlistBuddy -c "Set NSHumanReadableCopyright $MESSAGE"  ${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}
+1
3

eval 'ed ( ${}, ). , ; , (, , , ):

CURRENT_YEAR=$(date "+%Y")
MESSAGE="$(eval echo '"'"$(/usr/libexec/PlistBuddy -c "Print NSHumanReadableCopyright" Info.plist)"'"')"

( , '"' - ( , , eval ), "$(...)" PlistBuddy ( ), '"' - .

, PlistBuddy Set , , :

/usr/libexec/PlistBuddy -c "Set NSHumanReadableCopyright '${MESSAGE//\'/\'}'" ${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}

(, , $MESSAGE, .)

+1

, PlistBuddy , , , :

CURRENT_YEAR=$(date "+%Y")
MESSAGE=$(eval echo ...)

CURRENT_YEAR="$(date "+%Y")"
MESSAGE="$(eval echo ...)"

( $(...).)

, , , All . PlistBuddy.

+1

Try adding this right after the line MESSAGE=$(eval echo...:

printf -v MESSAGE "%q" "$MESSAGE"

This will keep the value $MESSAGEback to itself with characters like \, escaped.

0
source

All Articles