LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to save path names in variables in linux terminl (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-save-path-names-in-variables-in-linux-terminl-4175519313/)

jrs 09-18-2014 12:26 PM

how to save path names in variables in linux terminl
 
Shell: bash

I'm connecting to a remote linux machine via the shell.

My issue is that I'm constantly going back and forth between a few directories and its getting annoying to type out their names.

I know about tab complete, and I'm trying to explore !!, but right now theyre not as efficient as I want them to be.

I am also aware of the arrow functionality that goes through the command history.

I want to just save the paths in variables like this

myPath="/blah/dee/blah/files"

and then type the command

cd myPath.

Is this not possible, or am i missing something?

Or, how can I do something similar to this using !! or another command?

jrs 09-18-2014 12:33 PM

I am also aware of using the arrows to go through my command history

rigor 09-18-2014 12:46 PM

There are various possibilities, depending on the exact sequence of things in your situation. These include at least:
  1. cd -
  2. pushd / popd
  3. CDPATH

For example, if you are jumping back and forth between two directories A and B which both have the same parent directory, and you start in the parent directory:

Code:

cd A
cd ../B

naturally you would be in directory B.

If you then issue the command:
Code:

cd  -
you will be in directory A. If you issue the same command again, you'll jump back to directory B.

The commands pushd and popd as well as some related commands, will allow you to manipulate a directory stack of sorts, if that could suit your needs.

The CDPATH environment variable can be given a list of directories, much like the PATH variable. But each of the directories in the CDPATH variable can serve as the base for a set of directories which as a sort of shortcut form of the cd command; each of them can be cd'd into just giving the contained directory name to cd.

As an example, suppose a directory named files1 contains directories named A B C and D, a directory named files2 contains directories named V W X Y and Z.

Code:

export CDPATH=/blah/dee/blah/files1:/blah/dee/blah/files2
would allow you do this to go into the contained sub-directories of files1 or files2:

Code:

cd A
cd Z
cd C
cd W


Habitual 09-18-2014 12:58 PM

Quote:

Originally Posted by jrs (Post 5240377)
myPath="/blah/dee/blah/files"

and then type the command

cd myPath.

use
Code:

cd "$myPath"
You have to quote variables.
Tip: Quotes are only necessary if there are spaces in the directory names, eg: myPath="/path/to/some directory"

Code:

myPath="/tmp"
echo "$myPath"
/tmp
...
myPath=/tmp
echo "$myPath"
/tmp


but it is not required if there aren't any spaces the directory name. Both could be assumed 'correct' I guess.


All times are GMT -5. The time now is 11:43 AM.