Setting path in bash_profile - unix

Why does setting a PATH require the :${PATH} at the end?
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
When I append a path to this I would do
PATH=$PATH:...
How do I append a PATH without going to a new line. That is, how would I append a PATH to the original PATH statement.
If I wanted to put the following all in the first line, for instance. How does this interact with the :${PATH} part?
PATH=$PATH:/usr/local/mysql/bin

There's a difference between appending an existing path to the front or the end of the $PATH environment variable. The way bash resolves execution paths is by starting from the front of the list. That means if you have these two directories in your path:
PATH="/dir1/bin:/dir2/bin"
And they both have the executable test.sh in it, then when you run test.sh, it'll execute the one in /dir1/bin/test.sh since that directory appears first in the path.
Additionally, ${PATH} is the same as $PATH.
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
is simply adding /Library/Frameworks/Python.framework/Versions/2.7/bin to the front of the path and
PATH=$PATH:/usr/local/mysql/bin
is simply adding /usr/local/mysql/bin to the end of the path.
So how do you do both in one line? Something like this:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}:/usr/local/mysql/bin"

Related

How to move file from one directory to another (Julia)?

I have two different paths (directory path and file path). I need to move the file from one directory to another. How i should do it?
Use the mv function.
help?> mv
…
mv(src::AbstractString, dst::AbstractString; force::Bool=false)
Move the file, link, or directory from src to dst. force=true will first remove an existing dst. Return dst.
One thing to note is that both src and dest must be complete paths i.e. if your source file path is src = /home/me/file.txt and you want to move that to be under the directory path dstdir = /home/me/.julia, the call should be mv(src, joinpath(dstdir, basename(src)).
(Just to be clear, the arguments can be absolute paths or relative paths, either works. By "complete paths" I only mean that both src and dst have to include the file name, dst can't be just a directory unless you wish you overwrite the directory itself.)

How can I change PATH variable in zsh?

I want to change my PATH variable in zsh.
Problem: I don't understand where in the .zshrc file I have to make modifications.
Normally, I would look for the assignment to the PATH variable and set the values from scratch how I would like them to be (leaving all the systems binaries directories untouched).
The first lines in my .zshrc file are as follows:
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/Sam/oh-my-zsh"
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/13/bin
etc.
My actual PATH variable is:
/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/13/bin
I want to delete the directory where python3.8 is in, it's redundant.
My questions:
Do I have to change line 2 or line 7 in my .zshrc file?
Line 2 is commented out...is it executed anyway at the start of the terminal?
I have tried to comment out line 7. But the postgres directory still remained in my PATH variable which I don't understand.
The .zshrc is located in the home dir. The dot at the beginning keeps it hidden. Type ls -a from ~ directory to see it. To edit just run vim, nvim, etc as usual.
nvim ~/.zshrc
This is the command for Neovim. For your editor, sub nvim for the proper command.
Once you get in, you need only add the same export command that you would add from the command line.
export PATH=$PATH:/whatever/you/are/adding
EDIT
To remove a path variable:
First, run the command:
echo $PATH
from the command line.
Next, Copy the output to clipboard.
Finally, at the very end of the .zshrc file, add the following:
export PATH=<paste-what-you-copied-here>
Because you didn't reference $PATH after the =, this will set the path to EXACTLY what you pasted, no more, no less. Adding $PATH: as in the first example will just add whatever to the end of what is already there.
Since this gives you access to every item in the path array, deleting is just a matter of a literal highlight/select and of whatever you want deleted.
Finally, be sure that there is only one place in the file where you are editing PATH. If there are more than one, the result can be confusing to say the least.
That said, I believe the script runs top-to-bottom, so only the last mention should persist. You can take advantage of this in some situations, but for this purpose, one will suffice. XD
Be careful when you decide to fiddle with the PATH in .zshrc: Since the file is processed by every interactive subshell, the PATH would get longer and longer for each subshell, with the same directory occuring in it several times. This can become a nightmare if you later try to hunt down PATH-related errors.
Since you are using zsh, you can take advantage that the scalar variable PATH is mirrored in the array variable path, and that you can ask zsh to keep entries in arrays unique.
Hence, the first thing I would do is put a
typeset -aU path
in your .zshrc; this (due to mirroring) also keeps the entries in PATH unique. You can put this statement anywhere, but I have it for easier maintenance before my first assignment to PATH or path.
It is up to you to decide where exactly you add a new entry to PATH or path. The entries are searched in that order which is listed in the variable. You have to ask yourself two questions:
Are some directories located on a network share, where you can sometimes expect access delays (due to bad network conditions)? Those directories should better show up near the end of the path.
Do you have commands which occur in more than one directoryin your path? In this case, a path search will always find the first occurance only.
Finally, don't forget that your changes will be seen after zsh processes the file. Therefore, you could create a new subshell after having edited the file, or source .zshrc manually.

Finding and Replacing a filename in a css File with Bash script

I want to write a bash script that takes a user input (which will be a filename) and replaces a path to a file inside a css file with that filename. For simplicity, the two files will be in the same folder and in the css code only the filename at the end of the path should be changed.
I thought of using regex to match any line of code that has a specific pattern and then change the end of it. I know about sed, but since the filename always changes I'm not sure how to solve this problem other than regex. I also thought of adding a variable in the css file that holds the filename as a value and then adding that variable at the end of the path, but I'm not sure then how to access that variable from a bash script.
Any recommendations on how to tackle this problem?
Thanks!
Edit Adding more Information:
Here is the line in the css file I want to edit. The part to be changed is the fileName.png at the end. Since it will change I thought of using a regex to "find" the correct spot in the css file.
background: #2c001e url(file:////usr/share/backgrounds/fileName.png/);
A regex matching only this line in this specific file is the following. It could probably be simplified, but I don't see a reason why since it should work too:)
(background)\:\s\#.{6}\s(url)\((file)\:\/{4}(usr)\/(share)\/backgrounds\/.+\.(png)\/\)\;
So, there are some ways to do that. You can check topic in links below. sed command is also good idea. But before executing it, you can build a new variable (or multiple variables) to use them in regex sed -e syntax.
Getting the last argument passed to a shell script
Maybe, if you will add some input and output examples, I could be more specific in this case.
To replace the input in the file at run-time you could use this line in a script
sed "s/stringToReplace/$1/g" templateFile >fileToUse
the $1 is referencing the 2nd bash script argument (the first being $0, the name of the invoking script). stringToReplace would be written in verbatim in the templateFile.
You could also use a script with two runtime arguments ($1, $2), and you would change the original contents of the fileToUse using the -i option. But this requires storage of the last file path to be used as argument $1.

How to get absolute path on a zsh prompt?

I'm switching from bash to zsh.
I want to update my new zsh prompt and looked around to find a way, but I have only found "solutions" via oh-my-zsh.
The goal:
Location: ~/dir_1/dir_1_1/dir_1_1_1
What I have:
Location: dir_1_1_1
The code (source):
PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n#%m:"}%{$fg_bold[green]%}Location: %c%{$reset_color%}$(git_prompt_info) '
To preserve original prompt format (colors, git info and potentially other customisations before this one) except related to path info, you could append following to the end of ~/.zshrc:
PROMPT=${PROMPT/\%c/\%~}
As pointed out by #caleb-adams and #fend25 the key is replacing %c (just folder name) with %~ to include full path (or absolute from $HOME when under ~). See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html for more info
As Horacio Chavez mentioned in the comment above, you want to look here: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html for the details on how to change your displayed path in zsh.
In this case if you are looking for a path that is relative to your home folder, include a %~ in your zsh-theme file. Your prompt would now look like this:
PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n#%m:"}%{$fg_bold[green]%}Location: %~%{$reset_color%}$(git_prompt_info) '
note, I only changed one character in your prompt. the %c was swapped for the %~. %c will only give your current directory (see the document link above, or here)
For a full path you could use %/
Simplest way to add bash-style dir path to the prompt. Just add this to ~/.zshrc:
setopt PROMPT_SUBST
PROMPT='%n#%m: ${(%):-%~} '
The part with the path is ${(%):-%~}. Colouring could be added according with your lifestyle:)

Absolute path to Relative Path in Unix

This command was to list only the text files in my fileAsst directory:
ls ~/UnixCourse/fileAsst/*.txt
now I need to do the same thing but using a relative path instead, what I've tried so far is:
ls ~/UnixCourse/../*.txt
but it's saying I'm not getting the right answer, can anyone give me some hints or explain the differences between a relative path and absolute path, because I still dont under it.
Relative is always, well, relative to some existing directory, you are currently "located" in (by means of the cd command, usually). In your question you don't show what the current directory is. So there is no single "correct" answer.
If your current directory is, say, ~ (which is just a shortcut for your home directory, for example, /home/myuser), then you're relative ls command would look like (I'm adding the implied previous cd command for clarity):
cd ~
ls UnixCourse/fileAsst/*.txt
likewise if your current directory is ~/UnixCourse, then your relative ls command would look like:
cd ~/UnixCourse
ls fileAsst/*.txt
or the most simply case, when you are already in the directory you want to list the contents of:
cd ~/UnixCourse/fileAsst
ls *.txt
Get the idea?
Finally, as you have (accidentally, I'd assume) discovered, you can use .. and . in your paths, to imply "one (sub)directory up" or "the current directory".
For example, the following paths are equivalent and all resolve to "UnixCourse/fileAsst":
UnixCourse/../UnixCourse/fileAsst/
UnixCourse/SomeOtherDir/../fileAsst/
UnixCourse/./fileAsst
UnixCourse/fileAsst/YetAnotherDir/../
Note that this is a orthogonal concept and can be used with both, relative and absolute, paths.
There are two classes of pathname:
Absolute: start with a slash.
Relative: don't start with a slash.
Absolute pathnames have the same meaning regardless of your current working directory. An absolute pathname might contain .. components after the initial slash. An absolute pathname with no .. components that traverses no symlinks is sometimes known as a 'real path', after the system call realpath() that can be used to determine the real path of a name.
Relative pathnames are relative to the current working directory; conceptually, every relative pathname could be deemed to start with ./. A relative name might start with .. to move upwards from the current working directory. A special case of relative pathname is a simple filename — a name with no explicit directory component. That is a file (or other named object) in the current directory, of course.
To determine a relative pathname from an absolute pathname, you also have to know the current working directory.
For more information, see:
bash — Convert absolute path into relative path given a current directory
How to get relative path from absolute path — tagged .net and applying to Windows rather than Unix.
Absolute Path:
The absolute path is a path that contains the root directory and all other sub directories that contain a file or folder.
~/UnixCourse/fileAsst/*.txt
Relative path:
The relative path is only a portion of the full path.
cd ~UnixCourse/fileAsst/
ls *.txt
Didn't see a one-lined answer...
ls ../fileAsst/*.txt
Hope that can help someone!

Resources