Skip to main content
added 85 characters in body
Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

Don't try run commands stored in variables (unless it's just a simple command name and/or path). It is bound to fail in interesting ways unless you get the quoting right (which can be fiddly and lead to less robust code).

In your case, it would be enough with

cd "/rbyoko/$c/\$Ono.RCB" 

i.e. quote the directory pathname (to allow for $c to have whitespaces) and then escape the $.

Alternatively:

cd "/rbyoko/$c"/'$Ono.RCB' 

i.e. single quote the bit with the dollar sign to protect it from the shell.

Your function, modified:

visit_tree_recbin () { for dir in c d h l o t p w; do cd "/rbyoko/$dir"/'$Ono.RCB' printf 'We are now in %s\n' "$PWD" done } 

Alternatively,

visit_tree_recbin () { for dirpath in /rbyoko/[cdhlotpw]/'$Ono.RCB'/; do cd "$dirpath" printf 'We are now in %s\n' "$PWD" done } 

The difference is that the first variation will create directory pathnames and cd into these. This may fail if the directory does not exist.

The second variation uses a filename globbing pattern that matches existing directories. If the pattern matches anything, the cd will succeed (if you have permission to cd into the directory).

Note that bash maintains a PWD variable that contains the pathname of the current working directory, so there's no need to use pwd here.

Don't try run commands stored in variables (unless it's just a simple command name and/or path). It is bound to fail in interesting ways.

In your case, it would be enough with

cd "/rbyoko/$c/\$Ono.RCB" 

i.e. quote the directory pathname (to allow for $c to have whitespaces) and then escape the $.

Alternatively:

cd "/rbyoko/$c"/'$Ono.RCB' 

i.e. single quote the bit with the dollar sign to protect it from the shell.

Your function, modified:

visit_tree_recbin () { for dir in c d h l o t p w; do cd "/rbyoko/$dir"/'$Ono.RCB' printf 'We are now in %s\n' "$PWD" done } 

Alternatively,

visit_tree_recbin () { for dirpath in /rbyoko/[cdhlotpw]/'$Ono.RCB'/; do cd "$dirpath" printf 'We are now in %s\n' "$PWD" done } 

The difference is that the first variation will create directory pathnames and cd into these. This may fail if the directory does not exist.

The second variation uses a filename globbing pattern that matches existing directories. If the pattern matches anything, the cd will succeed (if you have permission to cd into the directory).

Note that bash maintains a PWD variable that contains the pathname of the current working directory, so there's no need to use pwd here.

Don't try run commands stored in variables (unless it's just a simple command name and/or path). It is bound to fail in interesting ways unless you get the quoting right (which can be fiddly and lead to less robust code).

In your case, it would be enough with

cd "/rbyoko/$c/\$Ono.RCB" 

i.e. quote the directory pathname (to allow for $c to have whitespaces) and then escape the $.

Alternatively:

cd "/rbyoko/$c"/'$Ono.RCB' 

i.e. single quote the bit with the dollar sign to protect it from the shell.

Your function, modified:

visit_tree_recbin () { for dir in c d h l o t p w; do cd "/rbyoko/$dir"/'$Ono.RCB' printf 'We are now in %s\n' "$PWD" done } 

Alternatively,

visit_tree_recbin () { for dirpath in /rbyoko/[cdhlotpw]/'$Ono.RCB'/; do cd "$dirpath" printf 'We are now in %s\n' "$PWD" done } 

The difference is that the first variation will create directory pathnames and cd into these. This may fail if the directory does not exist.

The second variation uses a filename globbing pattern that matches existing directories. If the pattern matches anything, the cd will succeed (if you have permission to cd into the directory).

Note that bash maintains a PWD variable that contains the pathname of the current working directory, so there's no need to use pwd here.

Source Link
Kusalananda
  • 356.6k
  • 42
  • 739
  • 1.1k

Don't try run commands stored in variables (unless it's just a simple command name and/or path). It is bound to fail in interesting ways.

In your case, it would be enough with

cd "/rbyoko/$c/\$Ono.RCB" 

i.e. quote the directory pathname (to allow for $c to have whitespaces) and then escape the $.

Alternatively:

cd "/rbyoko/$c"/'$Ono.RCB' 

i.e. single quote the bit with the dollar sign to protect it from the shell.

Your function, modified:

visit_tree_recbin () { for dir in c d h l o t p w; do cd "/rbyoko/$dir"/'$Ono.RCB' printf 'We are now in %s\n' "$PWD" done } 

Alternatively,

visit_tree_recbin () { for dirpath in /rbyoko/[cdhlotpw]/'$Ono.RCB'/; do cd "$dirpath" printf 'We are now in %s\n' "$PWD" done } 

The difference is that the first variation will create directory pathnames and cd into these. This may fail if the directory does not exist.

The second variation uses a filename globbing pattern that matches existing directories. If the pattern matches anything, the cd will succeed (if you have permission to cd into the directory).

Note that bash maintains a PWD variable that contains the pathname of the current working directory, so there's no need to use pwd here.