Prevent quote expansion in user-made function [duplicate]
This question already has an answer here:
Why does my shell script choke on whitespace or other special characters?
4 answers
When is double-quoting necessary?
1 answer
I have the following bash function :
tg() {
git add -A && git commit -m $1 && git push
}
But it seems not to work :
$ tg "create index for users"
error: pathspec 'index' did not match any file(s) known to git.
error: pathspec 'for' did not match any file(s) known to git.
error: pathspec 'users' did not match any file(s) known to git.
Obviously, the problem is that the quote was expanded and my middle command was read as git commit -m create index for users
rather than git commit -m "create index for users"
What did I do wrong ? How can I fix this ?
bash quoting
marked as duplicate by ilkkachu
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 10:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does my shell script choke on whitespace or other special characters?
4 answers
When is double-quoting necessary?
1 answer
I have the following bash function :
tg() {
git add -A && git commit -m $1 && git push
}
But it seems not to work :
$ tg "create index for users"
error: pathspec 'index' did not match any file(s) known to git.
error: pathspec 'for' did not match any file(s) known to git.
error: pathspec 'users' did not match any file(s) known to git.
Obviously, the problem is that the quote was expanded and my middle command was read as git commit -m create index for users
rather than git commit -m "create index for users"
What did I do wrong ? How can I fix this ?
bash quoting
marked as duplicate by ilkkachu
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 10:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47
add a comment |
This question already has an answer here:
Why does my shell script choke on whitespace or other special characters?
4 answers
When is double-quoting necessary?
1 answer
I have the following bash function :
tg() {
git add -A && git commit -m $1 && git push
}
But it seems not to work :
$ tg "create index for users"
error: pathspec 'index' did not match any file(s) known to git.
error: pathspec 'for' did not match any file(s) known to git.
error: pathspec 'users' did not match any file(s) known to git.
Obviously, the problem is that the quote was expanded and my middle command was read as git commit -m create index for users
rather than git commit -m "create index for users"
What did I do wrong ? How can I fix this ?
bash quoting
This question already has an answer here:
Why does my shell script choke on whitespace or other special characters?
4 answers
When is double-quoting necessary?
1 answer
I have the following bash function :
tg() {
git add -A && git commit -m $1 && git push
}
But it seems not to work :
$ tg "create index for users"
error: pathspec 'index' did not match any file(s) known to git.
error: pathspec 'for' did not match any file(s) known to git.
error: pathspec 'users' did not match any file(s) known to git.
Obviously, the problem is that the quote was expanded and my middle command was read as git commit -m create index for users
rather than git commit -m "create index for users"
What did I do wrong ? How can I fix this ?
This question already has an answer here:
Why does my shell script choke on whitespace or other special characters?
4 answers
When is double-quoting necessary?
1 answer
bash quoting
bash quoting
edited Jan 2 at 10:35
Rui F Ribeiro
40.7k1479137
40.7k1479137
asked Jan 2 at 9:53
Ewan DelanoyEwan Delanoy
1627
1627
marked as duplicate by ilkkachu
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 10:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ilkkachu
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 10:41
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47
add a comment |
1
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47
1
1
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47
add a comment |
1 Answer
1
active
oldest
votes
Double quote the expansion $1
:
tg() {
git add -A &&
git commit -m "$1" &&
git push
}
By not quoting $1
, the shell will split its value on whitespaces (the contents of $IFS
) and the resulting words will additionally undergo filename globbing.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shellecho "$MYVAR"
is equivalent toecho $MYVAR
?
– Ewan Delanoy
Jan 2 at 10:03
2
@EwanDelanoy You are not using any here-document in your code. A here-document is writtenutility <<TAG
and terminated byTAG
(TAG
can be any string). It's a form of redirection.echo $var
andecho "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test withvar=*
. When it comes to variable expansions and quoting,$var
and$1
behaves the same.
– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
@EwanDelanoy If there's only one "word" and no whitespaces, thenecho "$MYVAR"
andecho $MYVAR
will have same output. But if you havehello world
then"$MYVAR"
is treated as single word,$MYVAR
is split into two words. In this case, this is trivial. If you dols $MYVAR
it can give you file not found if you're looking forhello world
file, becausels
will look for two files,hello
andworld
, butls "$MYVAR"
will show you the filehello world
.
– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and thatIFS
has the default value.
– Kusalananda
Jan 2 at 10:13
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Double quote the expansion $1
:
tg() {
git add -A &&
git commit -m "$1" &&
git push
}
By not quoting $1
, the shell will split its value on whitespaces (the contents of $IFS
) and the resulting words will additionally undergo filename globbing.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shellecho "$MYVAR"
is equivalent toecho $MYVAR
?
– Ewan Delanoy
Jan 2 at 10:03
2
@EwanDelanoy You are not using any here-document in your code. A here-document is writtenutility <<TAG
and terminated byTAG
(TAG
can be any string). It's a form of redirection.echo $var
andecho "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test withvar=*
. When it comes to variable expansions and quoting,$var
and$1
behaves the same.
– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
@EwanDelanoy If there's only one "word" and no whitespaces, thenecho "$MYVAR"
andecho $MYVAR
will have same output. But if you havehello world
then"$MYVAR"
is treated as single word,$MYVAR
is split into two words. In this case, this is trivial. If you dols $MYVAR
it can give you file not found if you're looking forhello world
file, becausels
will look for two files,hello
andworld
, butls "$MYVAR"
will show you the filehello world
.
– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and thatIFS
has the default value.
– Kusalananda
Jan 2 at 10:13
|
show 1 more comment
Double quote the expansion $1
:
tg() {
git add -A &&
git commit -m "$1" &&
git push
}
By not quoting $1
, the shell will split its value on whitespaces (the contents of $IFS
) and the resulting words will additionally undergo filename globbing.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shellecho "$MYVAR"
is equivalent toecho $MYVAR
?
– Ewan Delanoy
Jan 2 at 10:03
2
@EwanDelanoy You are not using any here-document in your code. A here-document is writtenutility <<TAG
and terminated byTAG
(TAG
can be any string). It's a form of redirection.echo $var
andecho "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test withvar=*
. When it comes to variable expansions and quoting,$var
and$1
behaves the same.
– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
@EwanDelanoy If there's only one "word" and no whitespaces, thenecho "$MYVAR"
andecho $MYVAR
will have same output. But if you havehello world
then"$MYVAR"
is treated as single word,$MYVAR
is split into two words. In this case, this is trivial. If you dols $MYVAR
it can give you file not found if you're looking forhello world
file, becausels
will look for two files,hello
andworld
, butls "$MYVAR"
will show you the filehello world
.
– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and thatIFS
has the default value.
– Kusalananda
Jan 2 at 10:13
|
show 1 more comment
Double quote the expansion $1
:
tg() {
git add -A &&
git commit -m "$1" &&
git push
}
By not quoting $1
, the shell will split its value on whitespaces (the contents of $IFS
) and the resulting words will additionally undergo filename globbing.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Security implications of forgetting to quote a variable in bash/POSIX shells
Double quote the expansion $1
:
tg() {
git add -A &&
git commit -m "$1" &&
git push
}
By not quoting $1
, the shell will split its value on whitespaces (the contents of $IFS
) and the resulting words will additionally undergo filename globbing.
Related:
- Why does my shell script choke on whitespace or other special characters?
- When is double-quoting necessary?
- Security implications of forgetting to quote a variable in bash/POSIX shells
edited Jan 2 at 10:11
answered Jan 2 at 9:58
KusalanandaKusalananda
133k17254417
133k17254417
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shellecho "$MYVAR"
is equivalent toecho $MYVAR
?
– Ewan Delanoy
Jan 2 at 10:03
2
@EwanDelanoy You are not using any here-document in your code. A here-document is writtenutility <<TAG
and terminated byTAG
(TAG
can be any string). It's a form of redirection.echo $var
andecho "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test withvar=*
. When it comes to variable expansions and quoting,$var
and$1
behaves the same.
– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
@EwanDelanoy If there's only one "word" and no whitespaces, thenecho "$MYVAR"
andecho $MYVAR
will have same output. But if you havehello world
then"$MYVAR"
is treated as single word,$MYVAR
is split into two words. In this case, this is trivial. If you dols $MYVAR
it can give you file not found if you're looking forhello world
file, becausels
will look for two files,hello
andworld
, butls "$MYVAR"
will show you the filehello world
.
– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and thatIFS
has the default value.
– Kusalananda
Jan 2 at 10:13
|
show 1 more comment
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shellecho "$MYVAR"
is equivalent toecho $MYVAR
?
– Ewan Delanoy
Jan 2 at 10:03
2
@EwanDelanoy You are not using any here-document in your code. A here-document is writtenutility <<TAG
and terminated byTAG
(TAG
can be any string). It's a form of redirection.echo $var
andecho "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test withvar=*
. When it comes to variable expansions and quoting,$var
and$1
behaves the same.
– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
@EwanDelanoy If there's only one "word" and no whitespaces, thenecho "$MYVAR"
andecho $MYVAR
will have same output. But if you havehello world
then"$MYVAR"
is treated as single word,$MYVAR
is split into two words. In this case, this is trivial. If you dols $MYVAR
it can give you file not found if you're looking forhello world
file, becausels
will look for two files,hello
andworld
, butls "$MYVAR"
will show you the filehello world
.
– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and thatIFS
has the default value.
– Kusalananda
Jan 2 at 10:13
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shell
echo "$MYVAR"
is equivalent to echo $MYVAR
?– Ewan Delanoy
Jan 2 at 10:03
Thx for your help. I'm confused here : isn't "$1" supposed to be a "heredoc" string and variables inside it are supposed to be expanded ? For example, in the shell
echo "$MYVAR"
is equivalent to echo $MYVAR
?– Ewan Delanoy
Jan 2 at 10:03
2
2
@EwanDelanoy You are not using any here-document in your code. A here-document is written
utility <<TAG
and terminated by TAG
(TAG
can be any string). It's a form of redirection. echo $var
and echo "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test with var=*
. When it comes to variable expansions and quoting, $var
and $1
behaves the same.– Kusalananda
Jan 2 at 10:06
@EwanDelanoy You are not using any here-document in your code. A here-document is written
utility <<TAG
and terminated by TAG
(TAG
can be any string). It's a form of redirection. echo $var
and echo "$var"
are not equivalent for the same reasons as I mentioned in my answer. Test with var=*
. When it comes to variable expansions and quoting, $var
and $1
behaves the same.– Kusalananda
Jan 2 at 10:06
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
Got it, thx. I'll try your solution a few times and accept it if it works.
– Ewan Delanoy
Jan 2 at 10:08
1
1
@EwanDelanoy If there's only one "word" and no whitespaces, then
echo "$MYVAR"
and echo $MYVAR
will have same output. But if you have hello world
then "$MYVAR"
is treated as single word, $MYVAR
is split into two words. In this case, this is trivial. If you do ls $MYVAR
it can give you file not found if you're looking for hello world
file, because ls
will look for two files, hello
and world
, but ls "$MYVAR"
will show you the file hello world
.– Sergiy Kolodyazhnyy
Jan 2 at 10:09
@EwanDelanoy If there's only one "word" and no whitespaces, then
echo "$MYVAR"
and echo $MYVAR
will have same output. But if you have hello world
then "$MYVAR"
is treated as single word, $MYVAR
is split into two words. In this case, this is trivial. If you do ls $MYVAR
it can give you file not found if you're looking for hello world
file, because ls
will look for two files, hello
and world
, but ls "$MYVAR"
will show you the file hello world
.– Sergiy Kolodyazhnyy
Jan 2 at 10:09
1
1
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and that
IFS
has the default value.– Kusalananda
Jan 2 at 10:13
@SergiyKolodyazhnyy That first sentence is true only given that the word contains no filename globbing characters, and that
IFS
has the default value.– Kusalananda
Jan 2 at 10:13
|
show 1 more comment
1
Also: mywiki.wooledge.org/WordSplitting
– ilkkachu
Jan 2 at 10:47