-3

I have following variables

$a=/opt
$b=var
$c=usr
$path=/$a/$b/$c/man

when I do cd $path it says no such file or directory.

but this path actually exist /opt/var/usr/man .. any suggestion ?

  • Can you try echo $path. because you don't have / mentioned in the variable. its not working. – Mongrel Jan 6 '17 at 3:09
  • No it does not work – Manish Jan 6 '17 at 3:10
  • try this. echo $a$b$c – Mongrel Jan 6 '17 at 3:12
  • 1
    1. $path=/$a/$b/$c/man doesn't do what you think it does. Try path=test;$path=123 and see what error message it prints. 2. Why aren't you quoting your parameter expansions? – PM 2Ring Jan 6 '17 at 6:58
  • 1
    @JuliePelletier I said nothing about PATH or $PATH. I used lower-case $path in my example because that's what the OP used. – PM 2Ring Jan 6 '17 at 7:23
4

The problem is a bad syntax in variable assignments.

$a=/opt
$b=var
$c=usr
$path=/$a/$b/$c/man

.. will never work since variable assignments must not be preceded by $.

You should instead do it as follows:

a=opt
b=var
c=usr
dir=/$a/$b/$c/man
cd $dir

Note that I changed the final variable name as it is a bad idea to use two variables with the same name but in a different case as it can be confusing. (PATH being a very important environment variable)

-2

If you try this echo $a$b$c you will get this

[root@centseven ~]# echo $a$b$c
/optvarusr

So this is not a a execute.

Your last variable should be path=/$a$b$c/man

Change your variable to the below

$a=/opt
$b=/var
$c=/usr

Then try echo $path it works for me

[root@centseven ~]# echo $path
/opt/var/usr/man
  • There are 2 or 3 mistakes in this, but I will wait some clarifications from OP before I post my answer. – Julie Pelletier Jan 6 '17 at 3:18
  • I think OP haven't tried echo $path he is trying cd $path. – Mongrel Jan 6 '17 at 3:20
  • I tried both echo $path and cd $path none of them worked .. When I do echo $path it shows the correct path /opt/var/usr/man but My requriment is I need to do inside that directory thats why i am trying to cd command – Manish Jan 6 '17 at 3:22
  • @Manish: echo $path can not fail so if you say it didn't work, you have to say what it did. – Julie Pelletier Jan 6 '17 at 3:23
  • @Manish: Can you please show me the output of ls -ld /opt/var/usr/man? – Julie Pelletier Jan 6 '17 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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