Ways to provide arguments to a command executed by `bash -c`

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












In someone's reply to one of my posts (which I forgot), I remember



bash -c "somecommand $1" bash $somevariable


instead of



bash -c "somecommand $somevariable"


I saw this example again in findutils manual



find -exec sh -c 'something "$@"' sh ;


instead of



find -exec sh -c "something " ;


Do the two examples have the same reason to use the first solution instead of the other solution?
If yes, what is it?



Inspired Why does command injection not work in this example?
and Is the following the only form of command injection in bash?







share|improve this question

















  • 2




    The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
    – Tim Kennedy
    Jun 7 at 15:26










  • The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
    – schily
    Jun 7 at 15:52










  • @schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
    – ilkkachu
    Jun 7 at 19:10






  • 1




    AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
    – schily
    Jun 7 at 19:22






  • 1




    @schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
    – Stéphane Chazelas
    Jun 8 at 14:59















up vote
1
down vote

favorite












In someone's reply to one of my posts (which I forgot), I remember



bash -c "somecommand $1" bash $somevariable


instead of



bash -c "somecommand $somevariable"


I saw this example again in findutils manual



find -exec sh -c 'something "$@"' sh ;


instead of



find -exec sh -c "something " ;


Do the two examples have the same reason to use the first solution instead of the other solution?
If yes, what is it?



Inspired Why does command injection not work in this example?
and Is the following the only form of command injection in bash?







share|improve this question

















  • 2




    The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
    – Tim Kennedy
    Jun 7 at 15:26










  • The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
    – schily
    Jun 7 at 15:52










  • @schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
    – ilkkachu
    Jun 7 at 19:10






  • 1




    AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
    – schily
    Jun 7 at 19:22






  • 1




    @schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
    – Stéphane Chazelas
    Jun 8 at 14:59













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In someone's reply to one of my posts (which I forgot), I remember



bash -c "somecommand $1" bash $somevariable


instead of



bash -c "somecommand $somevariable"


I saw this example again in findutils manual



find -exec sh -c 'something "$@"' sh ;


instead of



find -exec sh -c "something " ;


Do the two examples have the same reason to use the first solution instead of the other solution?
If yes, what is it?



Inspired Why does command injection not work in this example?
and Is the following the only form of command injection in bash?







share|improve this question













In someone's reply to one of my posts (which I forgot), I remember



bash -c "somecommand $1" bash $somevariable


instead of



bash -c "somecommand $somevariable"


I saw this example again in findutils manual



find -exec sh -c 'something "$@"' sh ;


instead of



find -exec sh -c "something " ;


Do the two examples have the same reason to use the first solution instead of the other solution?
If yes, what is it?



Inspired Why does command injection not work in this example?
and Is the following the only form of command injection in bash?









share|improve this question












share|improve this question




share|improve this question








edited Jun 8 at 13:43
























asked Jun 7 at 15:15









Tim

22.5k61222401




22.5k61222401







  • 2




    The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
    – Tim Kennedy
    Jun 7 at 15:26










  • The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
    – schily
    Jun 7 at 15:52










  • @schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
    – ilkkachu
    Jun 7 at 19:10






  • 1




    AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
    – schily
    Jun 7 at 19:22






  • 1




    @schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
    – Stéphane Chazelas
    Jun 8 at 14:59













  • 2




    The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
    – Tim Kennedy
    Jun 7 at 15:26










  • The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
    – schily
    Jun 7 at 15:52










  • @schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
    – ilkkachu
    Jun 7 at 19:10






  • 1




    AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
    – schily
    Jun 7 at 19:22






  • 1




    @schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
    – Stéphane Chazelas
    Jun 8 at 14:59








2




2




The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
– Tim Kennedy
Jun 7 at 15:26




The answer is explained in the question you linked to in your question (unix.stackexchange.com/q/448443/674): ` The reason for this is that the ‘’ is expanded to a filename which might contain a semicolon or other characters special to the shell. If for example someone creates the file /tmp/foo; rm -rf $HOME then the two commands above could delete someone’s home directory.`
– Tim Kennedy
Jun 7 at 15:26












The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
– schily
Jun 7 at 15:52




The last find example will usually not work as POSIX only grants that simple arguments are expanded at all.
– schily
Jun 7 at 15:52












@schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
– ilkkachu
Jun 7 at 19:10




@schily, depends on what you mean with "usually". GNU find replaces anywhere in the strings, and according to documentation, so do the finds on FreeBSD and OpenBSD. So does the one that comes on Macs. I suppose those together are a significant portion of the finds in use.
– ilkkachu
Jun 7 at 19:10




1




1




AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
– schily
Jun 7 at 19:22




AIX does not expand it, HP-UX does not, Solaris does not, sfind/libfind does not. Looks like a 4:3 ration against expansion.
– schily
Jun 7 at 19:22




1




1




@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
– Stéphane Chazelas
Jun 8 at 14:59





@schily, AIX, HP-UX and Solaris AFAIK all have the the same AT&T find implementation and account for probably less than 1% of the systems in operation on topic on unix.stackexchange.com. GNU+busybox+freebsd+macOS probably account for more than 90% of the find implementations out there, and all do the expansions. (I'd rather they didn't though as that would get rid of a lot vulnerabilities in poorly written scripts). Or IOW, most people will have never come across an implementation that doesn't do the expansion.
– Stéphane Chazelas
Jun 8 at 14:59
















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448453%2fways-to-provide-arguments-to-a-command-executed-by-bash-c%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448453%2fways-to-provide-arguments-to-a-command-executed-by-bash-c%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)