0

I have a specific workspace that I must navigate to that is annoyingly long. like/really/damn/annoyingly/long/and/it/takes/a/while/to/type. I wanted to write a shell script that would just cd me into it, but of course, that won't work. Is there another way to make it so I can just cd into this directory without the hassle of typing it out?

Don't tell me copy and paste.

Thank you!

  • Are you trying to re-create the functionality of Windows 10, "command prompt here", from a pop-up menu? This is an already build-in feature in Gnome when you browse using Files. Highlight a folder, right click and choose Open in Terminal. – Bernard Wei Jul 24 '18 at 18:31
  • Read man bash, see CDPATH – waltinator Jul 24 '18 at 21:54

3 Answers3

5

You can't use a shell script to change directory, because the script runs in a different process and that child process cannot alter the environment of the current shell process.

You need to write a function or alias that will operate in your current interactive shell. One of these will work:

alias go='cd /really/damn/annoyingly/long/and/it/takes/a/while/to/type'
# or
go() { cd /really/damn/annoyingly/long/and/it/takes/a/while/to/type; }

Pick one, save it in your ~/.bashrc, type source ~/.bashrc and you should be ready to go.

glenn jackman
  • 17,900
1

Symlink

You could use a symlink, which is a special type of file that links to another file:

ln -s "/really/damn/annoyingly/long/and/it/takes/a/while/to/type" link_name

This creates a symlink called link_name which links to /really/damn/annoyingly/long/and/it/takes/a/while/to/type.

Then, in a shell:

~$ cd link_name
~/link_name$ 

Shell variable

Or create a variable:

variable_name='/really/damn/annoyingly/long/and/it/takes/a/while/to/type'

Put that in your ~/.bashrc, run source ~/.bashrc, then you can simply cd "$variable_name".

wjandrea
  • 14,236
  • 4
  • 48
  • 98
0

Actually you CAN cd in a script -- just put your cd command in a script. You have to execute it like this though: . ./script1. The . in front causes the script to run in your current shell instead of launching a new one.

~$ cat script1
cd ./ISO
~$ . ./script1
~/ISO$
wjandrea
  • 14,236
  • 4
  • 48
  • 98