-1

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 ?

7
  • Can you try echo $path. because you don't have / mentioned in the variable. its not working. Commented Jan 6, 2017 at 3:09
  • No it does not work Commented Jan 6, 2017 at 3:10
  • try this. echo $a$b$c Commented Jan 6, 2017 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? Commented Jan 6, 2017 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. Commented Jan 6, 2017 at 7:23

2 Answers 2

5

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
8
  • There are 2 or 3 mistakes in this, but I will wait some clarifications from OP before I post my answer. Commented Jan 6, 2017 at 3:18
  • I think OP haven't tried echo $path he is trying cd $path. Commented Jan 6, 2017 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 Commented Jan 6, 2017 at 3:22
  • @Manish: echo $path can not fail so if you say it didn't work, you have to say what it did. Commented Jan 6, 2017 at 3:23
  • @Manish: Can you please show me the output of ls -ld /opt/var/usr/man? Commented Jan 6, 2017 at 3:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.