How to print input arguments in bash script as is
Could you please help, does exist a command in bash to quote input argument?
script ./test.sh
:
#!/bin/bash
echo ${1}
./test.sh "It costs $1"
This prints It costs
, but how to print it as is It costs $1
.
Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1
. But how to quote it in the script?
UPDATED: It is possible with single quotes ./test.sh 'It costs $1'
bash quote
add a comment |
Could you please help, does exist a command in bash to quote input argument?
script ./test.sh
:
#!/bin/bash
echo ${1}
./test.sh "It costs $1"
This prints It costs
, but how to print it as is It costs $1
.
Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1
. But how to quote it in the script?
UPDATED: It is possible with single quotes ./test.sh 'It costs $1'
bash quote
1
Exactly as you just did:echo "It costs ${1}"
, or with single quotes:echo 'It costs ${1}'
.
– Amadan
Nov 26 '18 at 3:19
1
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted$1
– that other guy
Nov 26 '18 at 3:20
Did you mean to print the an argument, or that the cost is a single dollar?$1
will do the latter. If you run as-is with just./test.sh
you'll get what you're seeing, but try./test.sh "a buck"
. For real clarity, read this manual, especially here & here.
– Paul Hodges
Nov 26 '18 at 15:25
add a comment |
Could you please help, does exist a command in bash to quote input argument?
script ./test.sh
:
#!/bin/bash
echo ${1}
./test.sh "It costs $1"
This prints It costs
, but how to print it as is It costs $1
.
Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1
. But how to quote it in the script?
UPDATED: It is possible with single quotes ./test.sh 'It costs $1'
bash quote
Could you please help, does exist a command in bash to quote input argument?
script ./test.sh
:
#!/bin/bash
echo ${1}
./test.sh "It costs $1"
This prints It costs
, but how to print it as is It costs $1
.
Of course it is possible to quote the argument directly in the command:
./test.sh "It costs $1"
and it prints It costs $1
. But how to quote it in the script?
UPDATED: It is possible with single quotes ./test.sh 'It costs $1'
bash quote
bash quote
edited Nov 26 '18 at 3:53
Walrus
asked Nov 26 '18 at 3:17
WalrusWalrus
6418
6418
1
Exactly as you just did:echo "It costs ${1}"
, or with single quotes:echo 'It costs ${1}'
.
– Amadan
Nov 26 '18 at 3:19
1
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted$1
– that other guy
Nov 26 '18 at 3:20
Did you mean to print the an argument, or that the cost is a single dollar?$1
will do the latter. If you run as-is with just./test.sh
you'll get what you're seeing, but try./test.sh "a buck"
. For real clarity, read this manual, especially here & here.
– Paul Hodges
Nov 26 '18 at 15:25
add a comment |
1
Exactly as you just did:echo "It costs ${1}"
, or with single quotes:echo 'It costs ${1}'
.
– Amadan
Nov 26 '18 at 3:19
1
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted$1
– that other guy
Nov 26 '18 at 3:20
Did you mean to print the an argument, or that the cost is a single dollar?$1
will do the latter. If you run as-is with just./test.sh
you'll get what you're seeing, but try./test.sh "a buck"
. For real clarity, read this manual, especially here & here.
– Paul Hodges
Nov 26 '18 at 15:25
1
1
Exactly as you just did:
echo "It costs ${1}"
, or with single quotes: echo 'It costs ${1}'
.– Amadan
Nov 26 '18 at 3:19
Exactly as you just did:
echo "It costs ${1}"
, or with single quotes: echo 'It costs ${1}'
.– Amadan
Nov 26 '18 at 3:19
1
1
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted
$1
– that other guy
Nov 26 '18 at 3:20
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted
$1
– that other guy
Nov 26 '18 at 3:20
Did you mean to print the an argument, or that the cost is a single dollar?
$1
will do the latter. If you run as-is with just ./test.sh
you'll get what you're seeing, but try ./test.sh "a buck"
. For real clarity, read this manual, especially here & here.– Paul Hodges
Nov 26 '18 at 15:25
Did you mean to print the an argument, or that the cost is a single dollar?
$1
will do the latter. If you run as-is with just ./test.sh
you'll get what you're seeing, but try ./test.sh "a buck"
. For real clarity, read this manual, especially here & here.– Paul Hodges
Nov 26 '18 at 15:25
add a comment |
2 Answers
2
active
oldest
votes
You may update your program like below to print/display all arguments.
#!/bin/bash
echo "$@"
This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh
. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.
$@
prints all arguments.
2
Indeed, it is useful to doman bash
and read everything underEXPANSION
heading. There is so much good info there that many people never see.
– Amadan
Nov 26 '18 at 3:57
add a comment |
I often use:
printf "'%s' " "$@"
This is an alternative to echo when you want each argument quoted.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474365%2fhow-to-print-input-arguments-in-bash-script-as-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may update your program like below to print/display all arguments.
#!/bin/bash
echo "$@"
This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh
. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.
$@
prints all arguments.
2
Indeed, it is useful to doman bash
and read everything underEXPANSION
heading. There is so much good info there that many people never see.
– Amadan
Nov 26 '18 at 3:57
add a comment |
You may update your program like below to print/display all arguments.
#!/bin/bash
echo "$@"
This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh
. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.
$@
prints all arguments.
2
Indeed, it is useful to doman bash
and read everything underEXPANSION
heading. There is so much good info there that many people never see.
– Amadan
Nov 26 '18 at 3:57
add a comment |
You may update your program like below to print/display all arguments.
#!/bin/bash
echo "$@"
This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh
. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.
$@
prints all arguments.
You may update your program like below to print/display all arguments.
#!/bin/bash
echo "$@"
This will print all the arguments passed while running test.sh. $1 is a variable which substituted with empty string while calling your program test.sh
. Inside test.sh, first argument you passed in command line becomes $1, second becomes $2 and so forth.
$@
prints all arguments.
answered Nov 26 '18 at 3:28
Robert RanjanRobert Ranjan
34339
34339
2
Indeed, it is useful to doman bash
and read everything underEXPANSION
heading. There is so much good info there that many people never see.
– Amadan
Nov 26 '18 at 3:57
add a comment |
2
Indeed, it is useful to doman bash
and read everything underEXPANSION
heading. There is so much good info there that many people never see.
– Amadan
Nov 26 '18 at 3:57
2
2
Indeed, it is useful to do
man bash
and read everything under EXPANSION
heading. There is so much good info there that many people never see.– Amadan
Nov 26 '18 at 3:57
Indeed, it is useful to do
man bash
and read everything under EXPANSION
heading. There is so much good info there that many people never see.– Amadan
Nov 26 '18 at 3:57
add a comment |
I often use:
printf "'%s' " "$@"
This is an alternative to echo when you want each argument quoted.
add a comment |
I often use:
printf "'%s' " "$@"
This is an alternative to echo when you want each argument quoted.
add a comment |
I often use:
printf "'%s' " "$@"
This is an alternative to echo when you want each argument quoted.
I often use:
printf "'%s' " "$@"
This is an alternative to echo when you want each argument quoted.
answered Nov 26 '18 at 4:19
CraigCraig
5017
5017
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474365%2fhow-to-print-input-arguments-in-bash-script-as-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Exactly as you just did:
echo "It costs ${1}"
, or with single quotes:echo 'It costs ${1}'
.– Amadan
Nov 26 '18 at 3:19
1
When your script executes, it's already too late. There is nothing your script can do to get the original misquoted
$1
– that other guy
Nov 26 '18 at 3:20
Did you mean to print the an argument, or that the cost is a single dollar?
$1
will do the latter. If you run as-is with just./test.sh
you'll get what you're seeing, but try./test.sh "a buck"
. For real clarity, read this manual, especially here & here.– Paul Hodges
Nov 26 '18 at 15:25