1

This question already has an answer here:

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 .

marked as duplicate by David Foerster, Eric Carvalho, Zanna, Elder Geek, TheWanderer Oct 11 '16 at 0:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Running the script using . ./the/script.bash (aka sourcing the script) is exactly the way to get it to execute in the current shell. – steeldriver Oct 9 '16 at 11:41
  • 1
    Why mount it from a script when you can add entry for your window partition into /etc/fstab and it will mount during boot process ? – Sergiy Kolodyazhnyy Oct 10 '16 at 1:37
  • 1
    If it is in your PATH, you can just do . windows. You do not need to type the path to the script for sourcing with .. But: You can also start a shell in the script, which would, for most purposes, be enough. – muru Oct 10 '16 at 2:39
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

  • @DavidFoerster It was hard for me to understand exactly what OP was asking. However I see now that SteelDriver in comments below OP has been misled the same way as me. His comment got a +1. I'll delete if necessary my when/if OP clarifies. – WinEunuuchs2Unix Oct 9 '16 at 16:44
  • @WinEunuuchs2Unix I have updated the original question with a more precise example. Hopefully this will help clear up exactly what it is that I am asking. – blitz1616 Oct 10 '16 at 0:52
  • @blitz1616 Thank you for the clarification in the question. As such I've changed the answer accordingly. – WinEunuuchs2Unix Oct 10 '16 at 1:17
  • @DavidFoerster The OP changed the question and I've changed the answer accordingly. Please review your down-vote. – WinEunuuchs2Unix Oct 10 '16 at 1:31
  • Thanks for the notification and I agree with your answer. +1 – David Foerster Oct 10 '16 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.