2

I just learned about aliases in bash. I created one like so:

alias="cd $directory"

where $directory is from use input. In another shell script, I can launch a subshell like so:

( bash )

which brings me to the subshell, where, if I run cd, I go to the alias, cd $directory. This is great and it seems to be working as expected.

What I'm looking for is for when the subshell is launched, the cd happens automatically, so I tried:

( bash | cd )

thinking it would launch the subshell and cd to the user-entered $directorybut it's not working. How can I go about getting this to work? I also tried ( bash -c cd) to no avail.

Thanks.

nicorellius
  • 3,715
  • 4
  • 48
  • 79
  • This is off-topic for SO. It has nothing to do with programming - it's an operating system question. As such, it should be asked on [superuser](http://superuser.com) instead; SO is for programming related questions and answers. Please take a few minutes to review the [FAQ](http://stackoverflow.com/faq) for info on what questions are (and are not) suitable here. Thanks. :) – Ken White Mar 20 '12 at 02:37
  • Really? I thought bash scripting would be considered programming... There are many other bash related question on SO... – nicorellius Mar 20 '12 at 02:47
  • Bash scripting would be. Aliases are basic bash operations, not scripting. (Asking how to do something in a bash script is on-topic; asking how to change directories or list files in a bash shell isn't. This question falls into the latter category, IMO.) – Ken White Mar 20 '12 at 02:49
  • Fair enough. My apologies. I will ask in SU next time. Thanks for the heads up. – nicorellius Mar 20 '12 at 03:00

1 Answers1

3

The reason that ( bash | cd ) doesn't work is that each command in a pipeline is run in a separate subshell, so ( bash | cd ) is essentially equivalent to ( ( bash ) | ( cd ) ) (except that the latter launches even more subshells, of course). Instead, you should be able to write:

( cd ; bash )

(which runs cd before running bash) since bash will inherit a copy of the execution environment of the subshell it was launched from.

By the way — are you sure you want to create cd as an alias this way? That seems error-prone and confusing to me. I think it would be better to create a shell function that cds to the user-specified directory:

function cd_user () { cd "$directory" ; }

( cd_user ; bash )
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Yah, you're right; I don't want to create `cd` as an alias this way. I am just using it as an example. I would do something like `cd_udir` (like you point out). Your answer is very informative, too, by the way; thank you. Trying it now. – nicorellius Mar 20 '12 at 02:51
  • Your answer did solve my problem. Thanks. But I took your other recommendation and tried it and it didn't work. Could you look at my code and give me a hand with the function? I should have mentioned that the subshell is being called in another function within the script so I'm getting an error: `mmf.sh: line 79: cd_udir: command not found` – nicorellius Mar 20 '12 at 02:58
  • @nicorellius: There's no reason you shouldn't be able to call one function from another. :-/ – ruakh Mar 20 '12 at 03:01
  • you're right. I double checked my function and I had an typo in there. Thanks again for your help and patience ;-) – nicorellius Mar 20 '12 at 03:06