-1

I know these questions might have been asked somewhere but their solutions don't really work. I need help.

I want to add a variable to a directory name then change directory to it. The approach below did not work for me.

#!/bin/bash
targetVmwareVersion="15.5.6"
cd vmware-host-modules-workstation-$targetVmwareVersion

If I replace cd with ls, it lists contents of the folder on the terminal but the cd does not work.

I want the targetVmwareVersion to be concatenated after the name vmware-host-modules-workstation- then change directory.

I have tried the approach below but it didn't work.

#!/bin/bash
targetVmwareVersion="15.5.6"
path="vmware-host-modules-workstation-$targetVmwareVersion"
cd $path

The cd part does not work. The directory does not change.

. ./script does not work for me. The snippet above is part of my large script which depends on that snippet to proceed.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • . scriptname is also an option. – Artem S. Tashkinov Aug 19 '20 at 18:45
  • My problem is the directory does not change after concatenation – David Kariuki Aug 19 '20 at 20:21
  • 1
    Is that vmware-host-modules-workstation... directory in your current directory when you run . ./script? Do any errors get reported? – Jeff Schaller Aug 19 '20 at 23:02
  • I dont get any errors – David Kariuki Aug 19 '20 at 23:58
  • How is the code snippet invoked from the code that needs it to proceed? If it is invoked as a separate script, then I'm not surprised that it does not work as you can't change the current directory for a parent process from a child process. See e.g. https://unix.stackexchange.com/questions/27139/script-to-change-current-directory-cd-pwd – Kusalananda Aug 20 '20 at 07:25
  • This is the logic. I needed to concatenate the target version which is user input, to the name vmware-host-modules-workstation- so it would be something like vmware-host-modules-workstation- 15.5.6. I then wanted to change the directory to this folder now called vmware-host-modules-workstation- 15.5.6 and work on the files inside it. I'm doing all this in a single script not different. – David Kariuki Aug 20 '20 at 10:34
  • _"these questions might have been asked somewhere but their solutions don't really work." -- umm, so what solutions exactly didn't work? If you would tell that, it might make it easier to figure out where the problem is here. – ilkkachu Aug 20 '20 at 12:11
  • If I copy/paste that script into a directory that has vmware-host-modules-workstation-15.5.6 as a subdirectory and then execute . ./script, my current directory is changed to vmware-host-modules-workstation-15.5.6. I would caution you to avoid a variable named "path" (because it's close to "PATH") but your environment has something else that's interfering. – Jeff Schaller Aug 20 '20 at 12:44
  • @JeffSchaller, or you ever use zsh: http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-path – ilkkachu Aug 20 '20 at 12:56
  • @ilkkachu indeed; I did a quick test with zsh, but since cd is built-in, it succeeded anyway, so I'm stumped. – Jeff Schaller Aug 20 '20 at 12:59
  • 1
    @JeffSchaller, the problem isn't that, the problem is what it does to your PATH after that. – ilkkachu Aug 20 '20 at 15:05
  • @David I'm really confused why the cd command would silently fail. Can you please expand on "does not work" -- in your question? No errors at all? Perhaps you can wrap your tests (before and after) with pwd. – Jeff Schaller Aug 20 '20 at 16:54
  • The cd command did not work after concatenating that version to the name. I used ls in place of cd just to check if the folder exists or is been seeing and the ls command listed the contents of that folder. My problem was, I wanted that cd command to switch the directory on terminal and do something within that folder. The cd command did not change directory. I don't know if it opened a sub-shell. I'm not sure. – David Kariuki Aug 20 '20 at 17:40

1 Answers1

1

If you get no error, your path is changing, well, yours not, the shell's current path. I'll explain myself:

When you cd into another dir within a script, the script changes to that dir you're telling him to, but once the script finishes, you are in the same path because your prompt hasn't moved along the script.

They say one image is worth more than one thousand words, so here goes the demonstration:

Demo image of cd changing path within script

  • How can I make my script remain in that new directory. In my end the cd does not change directory and it gives no errors – David Kariuki Aug 20 '20 at 10:45
  • @DavidKariuki A child process (the script) can not change the working directory, or anything in the environment, of its parent process (the interactive shell). See e.g. https://unix.stackexchange.com/questions/27139/script-to-change-current-directory-cd-pwd – Kusalananda Aug 20 '20 at 11:00
  • I think that's not possible, since the script is a child process of the parent process which is the shell it is invoked from, the only way I can think of is invoking the script as a separate process, then cd into the directory and invoking another shell from inside the script, but that's not a proper way to do it. What do you actually wish to accomplish with this? What's your aim? – Alex López Aug 20 '20 at 11:05
  • I wanted to get into that directory, then extract some tar files from within the folder. – David Kariuki Aug 20 '20 at 17:42
  • I chose another work around. To copy contents of the folder to my current working directory then extract the tars then proceed. Thank you for the elaboration. I did not know a sub shell is opened when I run a script. – David Kariuki Aug 20 '20 at 17:43
  • Is there any other work around. Maybe changing the directory in the subshell then making the parent shell recognise the change? – David Kariuki Aug 20 '20 at 17:44
  • 1
    No, there is no way of having a subprocess change the parent's working directory since if this was posible it could lead to security breaches. – Alex López Aug 25 '20 at 09:37
  • Ok, thanks alot. – David Kariuki Aug 28 '20 at 01:55