writing a command in a file
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
I want the file to simply have run $(python -c "print('A'*256)")
inside of it.
I've tried adding it in a variable then passing it to a file by echo, that didn't work.
I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt
Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run
So it basically ignored $(python -c "print('A'*256)")
completely.
Any idea how to resolve this?
bash
add a comment |Â
up vote
1
down vote
favorite
I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
I want the file to simply have run $(python -c "print('A'*256)")
inside of it.
I've tried adding it in a variable then passing it to a file by echo, that didn't work.
I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt
Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run
So it basically ignored $(python -c "print('A'*256)")
completely.
Any idea how to resolve this?
bash
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
I want the file to simply have run $(python -c "print('A'*256)")
inside of it.
I've tried adding it in a variable then passing it to a file by echo, that didn't work.
I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt
Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run
So it basically ignored $(python -c "print('A'*256)")
completely.
Any idea how to resolve this?
bash
I'm having a difficulty writing a command in a file using a bash script (NOT the output of the command + I don't want the command to be executed)
I want the file to simply have run $(python -c "print('A'*256)")
inside of it.
I've tried adding it in a variable then passing it to a file by echo, that didn't work.
I also tried something like echo "run $(python -c "print('A'*256)")" > file.txt
Also didn't work, I actually got some errors so I assumed that the problem is because of the multiple double quotations, so I changed them to single quotations. That gave me this output in the file run
So it basically ignored $(python -c "print('A'*256)")
completely.
Any idea how to resolve this?
bash
asked Nov 4 '17 at 22:19
Jack
61
61
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
The most reliable way to get arbitrary strings into redirections is to use a here document:
cat > file.txt <<'EOT'
run $(python -c "print('A'*256)")
EOT
This will put that single line exactly as-is into file.txt
. The text up until EOT
is given to cat
as its standard input, and cat
just spits it out again to be redirected. The shell will keep reading the document until EOT
appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT
appears like that within your string, use something else instead - the delimiter is arbitrary).
Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.
1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.
add a comment |Â
up vote
0
down vote
Try to use escape characters instead:
echo run $(python -c "print('A'*256)") > file.txt
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
add a comment |Â
up vote
0
down vote
A string with *both kinds of quotes in it is always hard to handle.ÃÂ
There are three approaches:
Alternate between the two kinds of quotes.ÃÂ
Put the double quotes ("
) inside single quotes ('
) and vice versa.First break your string into three pieces:
run $(python -c "print ('A'*256) ")
âÂÂ---------1----------â âÂÂ---2---â âÂÂ3âÂÂwhere the first and third part contain no single quotes,
and the second part has no double quotes.ÃÂ
Then quote them appropriately:echo 'run $(python -c "print'"('A'*256)"'")'
âÂÂ----------1-----------âÂÂâÂÂ----2----âÂÂâÂÂ-3âÂÂjust running the substrings together.
Quote the string with double quotes,
and then use"
for every literal double quote
that you want to have echoed.ÃÂ
Also replace$
with$
echo "run $(python -c "print('A'*256)")"
An approach that works in bash only is to quote the string with
$'â¦'
.ÃÂ
Inside such a string, you can escape single quotes with'
:echo $'run $(python -c "print('A'*256)")'
Of course the > file.txt
part is as you would expect.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The most reliable way to get arbitrary strings into redirections is to use a here document:
cat > file.txt <<'EOT'
run $(python -c "print('A'*256)")
EOT
This will put that single line exactly as-is into file.txt
. The text up until EOT
is given to cat
as its standard input, and cat
just spits it out again to be redirected. The shell will keep reading the document until EOT
appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT
appears like that within your string, use something else instead - the delimiter is arbitrary).
Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.
1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.
add a comment |Â
up vote
3
down vote
The most reliable way to get arbitrary strings into redirections is to use a here document:
cat > file.txt <<'EOT'
run $(python -c "print('A'*256)")
EOT
This will put that single line exactly as-is into file.txt
. The text up until EOT
is given to cat
as its standard input, and cat
just spits it out again to be redirected. The shell will keep reading the document until EOT
appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT
appears like that within your string, use something else instead - the delimiter is arbitrary).
Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.
1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The most reliable way to get arbitrary strings into redirections is to use a here document:
cat > file.txt <<'EOT'
run $(python -c "print('A'*256)")
EOT
This will put that single line exactly as-is into file.txt
. The text up until EOT
is given to cat
as its standard input, and cat
just spits it out again to be redirected. The shell will keep reading the document until EOT
appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT
appears like that within your string, use something else instead - the delimiter is arbitrary).
Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.
1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.
The most reliable way to get arbitrary strings into redirections is to use a here document:
cat > file.txt <<'EOT'
run $(python -c "print('A'*256)")
EOT
This will put that single line exactly as-is into file.txt
. The text up until EOT
is given to cat
as its standard input, and cat
just spits it out again to be redirected. The shell will keep reading the document until EOT
appears at the start of the line, so you could put multiple lines in at once if you wanted (if EOT
appears like that within your string, use something else instead - the delimiter is arbitrary).
Because the delimiter is quoted at the top, no expansions are performed on the body of the here-document1 and all your characters are passed through untouched. The behaviour of here-documents is specified by POSIX and will work in any shell.
1There was a bug in some older versions of Bash that did perform some expansions in certain cases, but you won't hit that here.
answered Nov 5 '17 at 0:30
Michael Homer
42.6k6108148
42.6k6108148
add a comment |Â
add a comment |Â
up vote
0
down vote
Try to use escape characters instead:
echo run $(python -c "print('A'*256)") > file.txt
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
add a comment |Â
up vote
0
down vote
Try to use escape characters instead:
echo run $(python -c "print('A'*256)") > file.txt
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Try to use escape characters instead:
echo run $(python -c "print('A'*256)") > file.txt
Try to use escape characters instead:
echo run $(python -c "print('A'*256)") > file.txt
answered Nov 4 '17 at 23:17
à Âubham
844
844
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
add a comment |Â
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
IMHO, this is ugly and hard to get right (easy to get wrong).â¯â¯ But it does work, unlike the other answer.
â G-Man
Nov 4 '17 at 23:30
add a comment |Â
up vote
0
down vote
A string with *both kinds of quotes in it is always hard to handle.ÃÂ
There are three approaches:
Alternate between the two kinds of quotes.ÃÂ
Put the double quotes ("
) inside single quotes ('
) and vice versa.First break your string into three pieces:
run $(python -c "print ('A'*256) ")
âÂÂ---------1----------â âÂÂ---2---â âÂÂ3âÂÂwhere the first and third part contain no single quotes,
and the second part has no double quotes.ÃÂ
Then quote them appropriately:echo 'run $(python -c "print'"('A'*256)"'")'
âÂÂ----------1-----------âÂÂâÂÂ----2----âÂÂâÂÂ-3âÂÂjust running the substrings together.
Quote the string with double quotes,
and then use"
for every literal double quote
that you want to have echoed.ÃÂ
Also replace$
with$
echo "run $(python -c "print('A'*256)")"
An approach that works in bash only is to quote the string with
$'â¦'
.ÃÂ
Inside such a string, you can escape single quotes with'
:echo $'run $(python -c "print('A'*256)")'
Of course the > file.txt
part is as you would expect.
add a comment |Â
up vote
0
down vote
A string with *both kinds of quotes in it is always hard to handle.ÃÂ
There are three approaches:
Alternate between the two kinds of quotes.ÃÂ
Put the double quotes ("
) inside single quotes ('
) and vice versa.First break your string into three pieces:
run $(python -c "print ('A'*256) ")
âÂÂ---------1----------â âÂÂ---2---â âÂÂ3âÂÂwhere the first and third part contain no single quotes,
and the second part has no double quotes.ÃÂ
Then quote them appropriately:echo 'run $(python -c "print'"('A'*256)"'")'
âÂÂ----------1-----------âÂÂâÂÂ----2----âÂÂâÂÂ-3âÂÂjust running the substrings together.
Quote the string with double quotes,
and then use"
for every literal double quote
that you want to have echoed.ÃÂ
Also replace$
with$
echo "run $(python -c "print('A'*256)")"
An approach that works in bash only is to quote the string with
$'â¦'
.ÃÂ
Inside such a string, you can escape single quotes with'
:echo $'run $(python -c "print('A'*256)")'
Of course the > file.txt
part is as you would expect.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
A string with *both kinds of quotes in it is always hard to handle.ÃÂ
There are three approaches:
Alternate between the two kinds of quotes.ÃÂ
Put the double quotes ("
) inside single quotes ('
) and vice versa.First break your string into three pieces:
run $(python -c "print ('A'*256) ")
âÂÂ---------1----------â âÂÂ---2---â âÂÂ3âÂÂwhere the first and third part contain no single quotes,
and the second part has no double quotes.ÃÂ
Then quote them appropriately:echo 'run $(python -c "print'"('A'*256)"'")'
âÂÂ----------1-----------âÂÂâÂÂ----2----âÂÂâÂÂ-3âÂÂjust running the substrings together.
Quote the string with double quotes,
and then use"
for every literal double quote
that you want to have echoed.ÃÂ
Also replace$
with$
echo "run $(python -c "print('A'*256)")"
An approach that works in bash only is to quote the string with
$'â¦'
.ÃÂ
Inside such a string, you can escape single quotes with'
:echo $'run $(python -c "print('A'*256)")'
Of course the > file.txt
part is as you would expect.
A string with *both kinds of quotes in it is always hard to handle.ÃÂ
There are three approaches:
Alternate between the two kinds of quotes.ÃÂ
Put the double quotes ("
) inside single quotes ('
) and vice versa.First break your string into three pieces:
run $(python -c "print ('A'*256) ")
âÂÂ---------1----------â âÂÂ---2---â âÂÂ3âÂÂwhere the first and third part contain no single quotes,
and the second part has no double quotes.ÃÂ
Then quote them appropriately:echo 'run $(python -c "print'"('A'*256)"'")'
âÂÂ----------1-----------âÂÂâÂÂ----2----âÂÂâÂÂ-3âÂÂjust running the substrings together.
Quote the string with double quotes,
and then use"
for every literal double quote
that you want to have echoed.ÃÂ
Also replace$
with$
echo "run $(python -c "print('A'*256)")"
An approach that works in bash only is to quote the string with
$'â¦'
.ÃÂ
Inside such a string, you can escape single quotes with'
:echo $'run $(python -c "print('A'*256)")'
Of course the > file.txt
part is as you would expect.
edited Nov 4 '17 at 23:19
answered Nov 4 '17 at 23:08
G-Man
11.6k82657
11.6k82657
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f402559%2fwriting-a-command-in-a-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password