Reputation: 313
I want to customize a command to set up running environment, but I'm having some issue here. For example, I can run:
envsetup
and it will run the following script to help set up the environment:
cd /opt/dir/set_up | source environment
I have tried to add the following code to my $HOME .bashrc file, but it's not working (Maybe I should add it to the .bashrc in my root dir?):
alias envsetup = 'cd /opt/dir/set_up | source environment'
Could anyone let me know what might be wrong here please?
Upvotes: 0
Views: 157
Reputation: 58284
A couple of things:
cd
command changes directories and doesn't output anything to
standard output (stdout
). So there's no point in piping its output to another command with |
. If you want to execute two commands in sequence, you can separate with ;
.alias
you can't be too generous
with spaces.Here's an option, then, to do what I think you want:
alias envsetup='cd /opt/dir/set_up ; source environment'
alias envsetup='(cd /opt/dir/set_up ; source environment)'
This runs the commands in a subshell so your current shell setup is unchanged, including the current working directory.
Note that since this runs in a subshell, if source
is intended to change the environment variables in a persistent way, then this will not work. You can, alternatively, try something like this:
alias envsetup='p=$(pwd) ; cd /opt/dir/set_up ; source environment ; cd $p'
This will run in the same shell. You can choose whatever name you wish for p
.
Upvotes: 1