Score:-3

is it a good idea to use "sudo bash -c" instead of "sudo" forever?

kz flag
tmp

1st of all, we are talking about Debian/Ubuntu OS only.

2nd of all, we are talking about a non-root user with sudo privilege only.



So question is,

Is there any occasion that can not use sudo bash -c 'command' but only sudo command ?

is it a good idea to use sudo bash -c instead of sudo forever?

hr flag
I’m voting to close this question because it has been asked on multiple sites: [is it a good idea to use “sudo bash -c” instead of “sudo” forever?](https://unix.stackexchange.com/questions/666213/is-it-a-good-idea-to-use-sudo-bash-c-instead-of-sudo-forever)
tmp avatar
kz flag
tmp
@steeldriver I'm sorry about that, I'm new to here, I was so confused that which site my questions were supposed to belong to, it's always so hard to make a proper choice for me. but seems like I just got the "answer".
guiverc avatar
cn flag
Also note this is a Ubuntu site, for Ubuntu and *official* flavors of Ubuntu (ie. https://ubuntu.com/download/flavours) so your use of Debian pulled you off-tpoic for this site. Many of us may *love* and *use* Debian, but you should remain within site guidelines (you maybe accessing this site using your Ubuntu SSO/signon, but it's a Stack Exchange site, so questions should not be asked on other SE sites)
Score:1
cw flag

If you are not using bash, then bash -c runs the command with bash, and sudo command runs the command with your shell.

If you are using bash, then bash -c creates another bash for you to run your script in, which may actually cause problems regarding variables.

Try this example

S=2
echo $S
bash -c echo $S

You will get different output!

tmp avatar
kz flag
tmp
Thanks for your good example, just reminds me of the quote```"```, we usually use ```bash -c "echo $S" ``` with quote, and I actually get the same output in this case as well.
Shu Shang avatar
cw flag
Well, not using the bash -c version won't do you any harm and it won't cause you the trouble of quoting, which, if you happen to forget, can cause disaster.
bac0n avatar
cn flag
@ShuShang not sure what you are trying to describe, `-c` takes a 'command string' and, any following arguments will be assigned as positional parameters starting from `$0`, in this case, `$S` will become `$0` which is used as the name of the script, e.g., `bash -c nonexistent $S` will give you `2: line 1: nonexistent: command not found`.
bac0n avatar
cn flag
@tmp in most cases, you should use single quotes, or `$S` will be expanded by the current shell before `bash -c` interprets the command string. A general construct could look something like: `bash -c 'echo "$@"' my-bash 1 2 3 4`.
tmp avatar
kz flag
tmp
@bac0n But I still don't know why, in this example above, only bash -c " echo $S " or bash -c " echo ' $S ' " or bash -c " echo " $S " " will work, get the same outputs as echo $S = 2 , that would be another question of mine, what's exactly the quotes of bash -c itselves for? Do they effect straightly the command inside of them?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.