0

I have a long path /mnt/ABC/user/user123/domain1/computer1/My Document/ and it has multiple directories in it. I'm tried to type full path, and thinking to set up an environment variable in .profile to shorten it, but failed anyways. please help.

attempt 1:
adm@linux-lygy:~> export XYZS=/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ/DB

attempt 2:
adm@linux-lygy:~> export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ': No such file or directory
ls: cannot access 'Software/DB': No such file or directory

attempt 3:
adm@linux-lygy:~> export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software/DB
adm@linux-lygy:~> echo $XYZS/DB
/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\': No such file or directory
ls: cannot access 'Software/DB': No such file or directory

attempt 4:
adm@linux-lygy:~> export XYZS="\"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software\""
adm@linux-lygy:~> echo $XYZS/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ': No such file or directory
ls: cannot access 'Software"/DB': No such file or directory
adm@linux-lygy:~> echo "$XYZS"/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB
adm@linux-lygy:~> ls "$XYZS"/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"/DB': No such file or directory

attempt 5:
adm@linux-lygy:~> export XYZS="\"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software\""
adm@linux-lygy:~> echo $XYZS/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB
adm@linux-lygy:~> ls $XYZS/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\': No such file or directory
ls: cannot access 'Software"/DB': No such file or directory
adm@linux-lygy:~> echo "$XYZS"/DB
"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB
adm@linux-lygy:~> ls "$XYZS"/DB
ls: cannot access '"/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ\ Software"/DB': No such file or directory
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Me Okey
  • 15
  • 3
  • 2
    "I'm tried to type full path" -- you mean "I'm tired of typing the full path"? In any case, read the instructions of your shell. For Bash, `control-R` allows you to do a reverse search through history, just for example. In any case, if you have spaces, you will have to use quotes to keep the shell from splitting the commandline into two arguments at that space. – Ulrich Eckhardt May 10 '20 at 16:51

1 Answers1

3

Your second attempt was correct:

export XYZS="/mnt/ABC/user/user123/domain1/computer1/Personal/XYZ Software"

The problem was with how you were using the variable. You need to quote it since it contains spaces:

echo "$XYZS/DB"
ls "$XYZS/DB"

You should quote variables as a rule of thumb. See the Shellcheck wiki for details: Double quote to prevent globbing and word splitting

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Got it, thank you. I knew I should quote the variables but did not know that I should include the following path into the quotes. – Me Okey May 10 '20 at 17:22
  • 2
    @MeOkey Actually, quoting the whole thing is just a convention. It would also work if you did `"$XYZS"/DB`. In your attempts 4 and 5 the variable had the wrong value so the quoting was irrelevant to the problem. Welcome btw! – wjandrea May 10 '20 at 17:25
  • 1
    @MeOkey : You could consider switching to Zsh as your interactive shell, where quoting in this case is not necessary. – user1934428 May 11 '20 at 06:41
  • 1
    @user1934428 same for Fish – wjandrea May 11 '20 at 13:23