Prevent quote expansion in user-made function [duplicate]












1
















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 ?










share|improve this question















marked as duplicate by ilkkachu bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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
















1
















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 ?










share|improve this question















marked as duplicate by ilkkachu bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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














1












1








1









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 ?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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 bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

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














  • 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










1 Answer
1






active

oldest

votes


















4














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






share|improve this answer


























  • 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





    @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








  • 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








  • 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




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














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






share|improve this answer


























  • 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





    @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








  • 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








  • 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


















4














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






share|improve this answer


























  • 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





    @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








  • 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








  • 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
















4












4








4







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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 shell echo "$MYVAR" is equivalent to echo $MYVAR ?

    – Ewan Delanoy
    Jan 2 at 10:03






  • 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













  • 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, 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





    @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





















  • 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





    @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








  • 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








  • 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



















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





Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei