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
(cd "$A" && pwd && many && other && commands)
,A="NewPath"
then!-2
or 2 times Up arrow and enter...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
.CTRL+A
will put the cursor at the beginning of the line you just recalled with theUp
arrow. There are a lot of keyboard shortcuts for each terminal;-)