1

I have a bash script that is located in one of my PATH locations. When I run the script it ends with a cd /some/location but since the script is run in its own subproccess it doesn't change the location in the shell.

I know I can run the script using . ./the/script.bash and can even set that up as an alias but I would like to know if there is a way to get a command run from a script in my PATH to execute in the current shell.

--EDIT--

Apparently there was some confusion about my initial question so let me give an exact example.

I have a script called windows located at ~/bin/ and my .profile adds /home/$USER/bin to my PATH environment variable. The script windows mounts my windows partition (I dual boot) and then changed directories to the home file on my windows partition. The script looks like this:

#!/bin/sh

mount_point="/media/$USER/windows"
if ! grep -q "[[:space:]]$mount_point[[:space:]]" /proc/mounts; then
    sudo mount -t ntfs /dev/nvme0n1p3 "$mount_point"
fi
cd "$mount_point"/Users/Justin

So now when when I type the command windows into my terminal it runs this script, but since the script is not run in the current shell the cd does not change my current shells directory.

My question was is there a way to make this run in my current shell (so that when I run the command windows it changes my directory) without using an alias to call the script preceeded by a .

blitz1616
  • 223

1 Answers1

1

Based on new information in the OP this answer has been changed.

In your script you need to push the directory you want to end up in when the script finishes:

rick@dell:/$ cat /usr/local/bin/windows
pushd /boot/grub > /dev/null
ls

When you call the script you must but a dot and space in front of it . like this:

────────────────────────────────────────────────────────────────
rick@dell:/$ . windows
fonts             grub.cfg   grubenv  locale       zapgrub.cfg
gfxblacklist.txt  grub.cfg~  i386-pc  unicode.pf2
────────────────────────────────────────────────────────────────
rick@dell:/boot/grub$ 

Above is simple example of how calling . windows containing the push command works instead of calling windows containing cd command.

For your script you need to change cd "$mount_point"/Users/Justin to pushd "$mount_point"/Users/Justin and call the script with . windows instead of windows