DELETED: I don't understand this question.
Since it appears you want to change the working directory every once in a while, a suggestion would be to replace the alias with a shell function, which could take the directory pathname as a command line argument:
do_it () {
local wd="${1:-my_working_dir}"
cd "$wd" || return
do_stuff
}
This function takes an optional single argument, a directory pathname. If given, the function's local wd
variable is set to this value. If not given, the local wd
variable is set to the static string my_working_dir
.
With a small change, you could disallow the function from leaving the user in an unexpected working directory:
do_it () {
local wd="${1:-my_working_dir}"
(
cd "$wd" || return
do_stuff
)
}
The parenthesised subshell's environment inside the function (which includes the current working directory, possibly modified by cd
) would not affect the environment of the calling shell.
You would declare this function wherever you usually declare aliases, and you would use it either as
do_it
or with an argument,
do_it some/directory/path
or, in a loop that runs it over a number of directory pathnames,
for target_dir in some/pattern/dir*/
do
do_it "$target_dir"
done