简体   繁体   中英

How to run bash script inside another bash script where path has spaces?

Hi I am trying to call a bash script from inside another bash script. Here's how the structure is:

script a: a.sh <pass_a_path_with_spaces>

example:

a.sh /Users/Desktop is this/

Now I want to run a.sh inside b.sh like this script b:

b.sh <pass_a_path_with_spaces>

example:

b.sh

path="/Users/Desktop\ is\ this/"
script_a="/Users/Desktop\ is\ this/a.sh"
source "$script_a" "$path"

However when I run this, I get this error: /Users/Desktop\ is\ this/a.sh: No such file or directory

Can someone point out where I am going wrong?

Thank you!

You need to double quote around the directory with the spaces to preserve the name of the directory, something like this:

b.sh

  path=/Users/"Desktop is this"/
  script_a=/Users/"Desktop is this"/a.sh
  source "$script_a" "$path"

Use a quote ' because this is lateral and the string inside the single quote, will not be changed when the variable is used.

for example, a.sh '/Users/Desktop is this/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM