Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two command at once in bash.profile

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
hi there,
I was wondering if it was possible to execute two commands at once and set it as an alias in bash_profile.

I want to have a single command to do
"sudo -H -u user1 bash" and then "cd ~" in one command and have it set as an alias in bash_profile.


I saw something on the internet that went
alias suuser1 ='sudo -H -u user1 bash; cd ~' but that didn't quite work.

Is my only option to write a seperate shell script for this?
 
So... you want to [tt]sudo bash[/tt], then [tt]cd[/tt] to the home directory not in the original shell, but in the shell you just started?

See why a single alias or even a single shell script isn't gonna do anything for you, here?


Are you looking for [tt]sudo -i[/tt]?
 
What I am trying to do is to do "sudo -H -u user1 bash"
at this point, if I type ls, I will get permission denied since I am still in the previous user's home dir.

Is there a sudo -i option.
my man sodo does not seem to show so...
sudo -V | -h | -L | -l | -v | -k | -K | [-H] [-P] [-S] [-b] [-p prompt]

I guess I will have to write a function as stefanwangner pointed out.

Thanks
 
Your [tt]sudo[/tt] probably doesn't have the option. Mine doesn't either, but newer versions apparently do.


You didn't understand the main point of my post. I was explaining why you're not going to find what you're looking for.

You can't run an alias (or program or function or shell script or shell-builtin) that does one thing in one shell, then another thing in another, different, separate shell.

Your first command, [tt]sudo bash[/tt], which is run from the original shell, starts a second shell. You can't run a command in that second shell by typing it to the first one.


Instead, you'll need to tell either [tt]sudo[/tt] or [tt]bash[/tt] to run the [tt]cd[/tt] command.

With [tt]sudo[/tt], the [tt]-i[/tt] option is one way to do it. There may be other ways that work with your version of [tt]sudo[/tt].

To do it through [tt]bash[/tt], you might try something like
Code:
sudo -H -u user1 bash -c "cd ~; exec bash -l"
That's off the top of my head, and there's probably better way to do it.
 
Thanks for the input chipperMDW
now I see what you meant.
Yeah I think my sudo is old (does not have -i nor -c).
I am looking around now to see if there is a wrok-around for this as I don't have the prev to install a newer version of sudo.
 
That [tt]-c[/tt] is meant for [tt]bash[/tt], not for [tt]sudo[/tt]. I think [tt]-c[/tt] existed back in the 70s with the original Bourne shell, so your [tt]bash[/tt] definitely isn't too old to have it.


Did you try that command? It does exactly what you want, as far as I can tell.

If you really did try it and [tt]sudo[/tt] tried to interpret the [tt]-c[/tt] itself, you may need to quote the entire bash command.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top