2

I'd like to do something like this:

(cd "$1" && pwd && many && other && commands) < echo "../path"

Which should take the arguments provided and pass them to the subshell. The subshell should then execute.

The important part is that passed arguments are at the end of command line to allow for easy altering because the cursor is at the end of command when user presses the Up arrow.

7
  • A subshell will take the variable just defined in the parent shell. So (cd "$A" && pwd && many && other && commands), A="NewPath" then !-2 or 2 times Up arrow and enter...
    – Hastur
    Commented Jan 25, 2017 at 12:18
  • Nice to know that subshell will take the variable just defined in the parent shell. But your command does not work.
    – meridius
    Commented Jan 25, 2017 at 15:40
  • I've tested on a bash 4.3.11. They work. Where did you find they are not working? Did you set the first time the variable A to the old path? Give it a look to the answer below and let me know. Else you can pass parameter to a bash invocation with the option -c.
    – Hastur
    Commented Jan 25, 2017 at 15:46
  • Sorry. What I meant to say is that it executes the subshell even without var A being set. I need to set the var and run the subshell in one go.
    – meridius
    Commented Jan 25, 2017 at 15:50
  • I supposed you want to reiterate the same command more times changing the pah (for example). So the 1st time is set and after you run it again. BTW you may be interested to know that CTRL+A will put the cursor at the beginning of the line you just recalled with the Up arrow. There are a lot of keyboard shortcuts for each terminal ;-)
    – Hastur
    Commented Jan 25, 2017 at 16:02

1 Answer 1

2

A bash subshell will inherit the variables of the parent shell (but it will not modify them!).
A simple way is to define you series of command with variables that you define and modify them in the parent shell.

MyTempVar="FirstPath"
(cd "$MyTempVar" && pwd && many && other && commands) && unset MyTempVar
MyTempVar="NewPath"
!-2      
  • With !-2, if history options are enabled, you will execute the second last command of your history. It is an expansion of the built-in history of bash.
  • More safe is to enable the histverify with shopt -s histverify to have a visual confirmation before that the command from the history is processed.
  • Again safe it is Up + Up + Enter.
  • With unset MyTempVar you can optionally unset the variable MyTempVar if you want.

You can even create a function or a script and pass the variables to them. E.g.

TTT(){ (A="$1"; cd "$A" && pwd ;)  }

then call

TTT MyPath
6
  • The functional approach looks promising. But I'm wondering why do I have to enclose the commands in subshell brackets if they are in function already?
    – meridius
    Commented Jan 25, 2017 at 15:51
  • To avoid to modify the value of A in the parent shell. The subshell was your request... :-) BTW you do not need even to set A, as you can read you can use $1..$n inside the function. And you need not to use a subshell too. You can do a script and put it executable (chmod u+x myscript.sh) somewhere in your path ($PATH). When you need to modify the variable in the parent shell you can source it, else you can execute it.
    – Hastur
    Commented Jan 25, 2017 at 15:55
  • I know that I could create a script but I'd like to avoid that since I know I won't be using created command in the future. Also it's an unnecessary hassle to edit text file. What I was going for when asking about brackets was that when you do AAA(){ echo $1 && pwd } ; AAA foo the shell enters to "input mode". But with AAA(){ ( echo $1 && pwd ) } ; AAA foo it doesn't. I find that strange.
    – meridius
    Commented Jan 25, 2017 at 16:07
  • What do you mean for "input mode"?
    – Hastur
    Commented Jan 25, 2017 at 21:38
  • 1
    Now I understand. It waits you finish the command (or you asked to have another line or you close bad brackets). You need to put a ;. In this wayAAA(){ cd $1 && pwd ; } it is ok. Instead BBB(){ cd $1 && pwd } will wait for something. If you add } and press enter, when you reload the last command (with the Up key) you will find BBB(){ cd $1 ; pwd }; }. Now the first } is taken as parameter of pwd that gives you no error. Try with CCC(){ cd $1 ; echo } then, when asked, with another }. After execute it (CCC mydir) and it will change directory and print the character }.
    – Hastur
    Commented Jan 26, 2017 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .