![]() It looks simple and straightforward enough, but there are lots of small devils in the details, that can come out and haunt you. When you use the variable, you prefix a $ symbol: > echo $name In shell scripts (sh, bash, and zsh) you use the equals character = to assign a string value to a variable: > name=JohnĪs long as there are no special characters in the literal string, there is no need to quote the string. When the literal string contains special characters, you need to either escape the special characters with the backslash \ or quote the entire string with either single quotes ' or double quotes ". ( More details in this post.) name='John Doe' Space is proverbial ‘killer character’, especially for file paths. The difference between single quotes and double quotes is important. Single quotes escape every special character except the single quote itself. > echo "The Computer Name is `scutil -get ComputerName`" This allows us to use old-style command substitution with backticks and variable substitution (dollar sign) within double quoted strings: > echo "Hello, $name" (There are slight differences between the shells on this.) A single quoted string of '#$"\!' will represent exactly those characters.ĭouble quotes escape most characters, except the double quote " the backtick `, the dollar sign $, the backslash \, and the exclamation mark !. Though you should be using the $(…) syntax for command substitution instead of backticks `. The parenthesis syntax is more readable and can be nested. In general, it is a good rule to always quote literal strings. Combining literal strings with special characters Whether you should use double quotes or single quotes depends on the use case. Things can start getting complicated when you want special characters with their special functionality. For example, when you want to refer to the path ~/Library/Application Support, you should put it in quotes, because of the space. But when you put the ~ in the quotes, it will not be substituted to the user’s home directory path. There are a few ways to solve this problem. You could escape the space with a backslash. But the easiest is to move the special character out of the quotes: dirpath=~"/Library/Application Support" You could use the $HOME variable instead (but be sure you are in a context where this is set). A common situation for MacAdmins is the following osascript: osascript -e 'display dialog "Hello, World"' Sometimes it is necessary to have a set of quotes within quotes. The osascript command can be used to run Apple commands or scripts. Since AppleScript uses double quotes for literal strings, the entire AppleScript command is passed in single quotes. This works fine, until you want to do something like this: computerName=$(scutil -get ComputerName) This keep the command string together and the double quotes in single quotes don’t confuse the shell. NewName=$(osascript -e 'text returned of (display dialog "Enter Computer Name" default answer "$computerName")')Īgain, we put the AppleScript command in single quotes, so we can use double quotes inside. There are a few solutions out of this, I will demonstrate three:įirst, you could close the single quotes before the variable substitution and re-open them after: osascript -e 'text returned of (display dialog "Enter Computer Name" default answer "'$computerName'")' But now, the single quotes are also blocking the variable substitution and we get the literal $computerName in the dialog. ![]() This will in this form as long as $computerName contains no spaces. This is unlikely as the default computer name is something like Armin's MacBook Pro. We can avoid that by putting the substitution itself in double quotes: osascript -e 'text returned of (display dialog "Enter Computer Name" default answer "'"$computerName"'")' The shell will consider that space a separator before a new argument, breaking the AppleScript command into meaningless pieces and failing the osascript command. This works and is entirely legal syntax, but not very legible. But then we have to deal with the double quotes required for the AppleScript string literal.Įscaping the escape charactersĪnother solution is to use double quotes for the entire AppleScript command, we can use variable substitution inside.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |