What are you trying to do?
If the user tries from cd to /Volumes/Storage/Users/testuser/Desktop/folder1/folder2/ and their directory is HOME /home/bob , should they cd instead of /home/bob/Desktop/folder1/folder2 ?
What to do if /home/bob/Desktop/folder1/folder2 does not exist?
Here's a function that replaces cd with one that places the directory you are burning the CD to. The alias _cd is cd and you are all set up.
The same basic thing can be done in other shell scripts if you do not intercept the cd .
I use the syntax ${parameter#word} to remove the $BAD_DIR prefix. I use glob matching to see if the directory has the wrong directory as a prefix.
And then I use HOME_DIR=~ to set my real HOME directory. I do not know what will happen if the user goes to $HOME , if he changes ~ or not. However, this allows me to use quotation marks in the name of my directory.
I should probably check $PWD to make sure they don't exist yet and check if it is relative cd vs where the full path is given. However, this is easy enough to add. That should be enough to get you started.
function _cd { cd_to_dir="$1" BAD_DIR="/Volumes/Storage/Users/testuser" if [[ $cd_to_dir = $BAD_DIR* ]] then HOME_DIR=~ cd_to_dir="$HOME_DIR/${cd_to_dir#${BAD_DIR}}" fi \cd "$cd_to_dir" } alias cd=_cd
This works on my computer . I have BASH, but I also installed extglob and a few other things. This SHOULD work without extglob , but if it does not add these lines:
function _cd { cd_to_dir="$1" BAD_DIR="/Volumes/Storage/Users/testuser" is_extglob_set=$(shopt -q extglob) [ $is_extglob_set ] || shopt -s extglob if [[ $cd_to_dir = $BAD_DIR* ]] then HOME_DIR=~ cd_to_dir="$HOME_DIR/${cd_to_dir#${BAD_DIR}}" fi [ $is_extglob_set ] || shopt -u extglob \cd $cd_to_dir } alias cd=_cd
source share