Print a string including single quotes and other special characters
Solution 1
You need to either keep using single quotes, but then print out the ones you need in the output "separately", or use double quotes and escape the dollar signs.
For the second option:
print "clock=\$(prtconf -s | awk '{print \$4,\$5}')" > test.txt
For the first:
print 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt
(That's 'text'
then escaped single quote \'
then 'other text'
.)
For the sake of completeness, note that print
expands backslash-character escape sequences (this doesn't matter in your case because the string you want to print doesn't contain any backslash). To avoid this, use print -r
.
Solution 2
The simple way to do it is to use single quotes '…'
around the string. Single quotes delimit a literal string, so you can put it anything between them except a single quote '
. To insert a single quote in the string, use the four-character sequence '\''
(single quote, backslash, single quote, single quote).
Technically, there's no way to put a single quote inside a single-quoted literal. However consecutive literals are just as good as a single literal. 'foo'\''bar'
is parsed as
- the single-quoted literal
foo
- the backslash-quoted literal character
'
- the single-quoted literal
bar
This effectively means that '\''
is a way to escape a single quote in a single-quoted literal.
Note that the ksh print
command performs backslash expansion. Add the -r
option to avoid this. It won't hurt you because there happens to be no backslash in the string you want to print, but it's better to use -r
, in case a backslash is added during maintenance.
print -r -- 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt
Alternatively, you can use the POSIX method to print a string literally with a newline at the end:
printf '%s\n' 'clock=$(prtconf -s | awk '\''{print $4,$5}'\'')' > test.txt
Solution 3
Why not just use a here document?
cat <<'END' > test.txt
clock=$(prtconf -s | awk '{print $4,$5}')
END
Note the quotes around END
in <<'END'
: they mean that the text in the following lines (until the END
marker) will be used literally. With just <<END
, dollar expansion (i.e. $(command)
or $variable
) would be performed.
Solution 4
2 solution is there as @Mat told.
1] Using single quote '
: this single quote removes special meaning of escape characters,$
, different commands (basically all bash constructs & unix commands). print
command will take everything as plain characters and writes this to file test.txt
2] Using double quotes "
: Here we need to explicitly escape the bash constructs. \$
removes the special meaning of $
, i.e. value of variable used after $
.
Related videos on Youtube
batil
Seeking answers to all complicated questions, an amateur to UNIX environment.
Updated on September 18, 2022Comments
-
batil almost 2 years
How can I write this with an AIX script correctly? My requirement is to write this command in test.txt:
clock=$(prtconf -s | awk '{print $4,$5}')
I tried this command:
print 'clock=$(prtconf -s | awk '{print $4,$5}')' > test.txt
Output written in test.txt gives me:
clock=$(prtconf -s | awk {print ,})
If I use
" "
quotes:print "clock=$(prtconf -s | awk '{print $4,$5}')"
It gives me directly to:
clock=3612 MHz
How can I solve this?
-
cuonglm about 10 yearsDo you mean your want to use
clock=$(prtconf -s | awk '{print $4,$5}')
expression inawk
statement?
-
-
Mat about 10 yearsUnless I've misunderstood the question, OP wants the command to be printed in the text file, not the result (if it's the result, their second attempt is just missing a pair of quotes).
-
cuonglm about 10 yearsOh,sorry, my misreading, remove comment.