How to change path name in mac bash? - bash

I want to change the path?(they are id?) in front of %, because they are too long to read.
I want to make them short.

In your ~/.bash_profile or ~/.bashrc, using any editor you like, add one line
export PS1="<whatever prompt you like>"
Then restart the bash.

Related

zsh: command not found: symfony on ubuntu 20 [duplicate]

I'm using zsh terminal, and I'm trying to add a new entry (/home/david/pear/bin) to the PATH variable. I don't see a reference to the PATH variable in my ~/.zshrc file, but doing echo $PATH returns:
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
So I know that the path variable is being set somewhere. Where is the PATH variable set / modified for the zsh terminal?
Actually, using ZSH allows you to use special mapping of environment variables. So you can simply do:
# append
path+=('/home/david/pear/bin')
# or prepend
path=('/home/david/pear/bin' $path)
# export to sub-processes (make it inherited by child processes)
export PATH
For me that's a very neat feature which can be propagated to other variables.
Example:
typeset -T LD_LIBRARY_PATH ld_library_path :
Here, add this line to .zshrc:
export PATH=/home/david/pear/bin:$PATH
EDIT: This does work, but ony's answer above is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH. This approach is standard for bash, but as far as I know, there is no reason to use it when ZSH provides better alternatives.
You can append to your PATH in a minimal fashion. No need for
parentheses unless you're appending more than one element. It also
usually doesn't need quotes. So the simple, short way to append is:
path+=/some/new/bin/dir
This lower-case syntax is using path as an array, yet also
affects its upper-case partner equivalent, PATH (to which it is
"bound" via typeset).
(Notice that no : is needed/wanted as a separator.)
Common interactive usage
Then the common pattern for testing a new script/executable becomes:
path+=$PWD/.
# or
path+=$PWD/bin
Common config usage
You can sprinkle path settings around your .zshrc (as above) and it will naturally lead to the earlier listed settings taking precedence (though you may occasionally still want to use the "prepend" form path=(/some/new/bin/dir $path)).
Related tidbits
Treating path this way (as an array) also means: no need to do a
rehash to get the newly pathed commands to be found.
Also take a look at vared path as a dynamic way to edit path
(and other things).
You may only be interested in path for this question, but since
we're talking about exports and arrays, note that
arrays generally cannot be exported.
You can even prevent PATH from taking on duplicate entries
(refer to
this
and this):
typeset -U path
PATH pre-populated
The reason your path already has some entries in it is due to your system shell files setting path for you. This is covered in a couple other posts:
Why and where the $PATH env variable is set?
Where is the source of $PATH? I cannot find it in .zshrc
one liner, without opening ~/.zshrc file
echo -n 'export PATH=~/bin:$PATH' >> ~/.zshrc
or
echo -n 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc
To see the effect, do source ~/.zshrc in the same tab or open a new tab
Added path to ~/.zshrc
sudo vi ~/.zshrc
add new path
export PATH="$PATH:[NEW_DIRECTORY]/bin"
Update ~/.zshrc
Save ~/.zshrc
source ~/.zshrc
Check PATH
echo $PATH
OPTION 1: Add this line to ~/.zshrc:
export "PATH=$HOME/pear/bin:$PATH"
After that you need to run source ~/.zshrc in order your changes to take affect OR close this window and open a new one
OPTION 2: execute it inside the terminal console to add this path only to the current terminal window session. When you close the window/session, it will be lost.
If you are on macOS (I'm on Monterey 12.3.1), you may have been pulling your hair like I did metaphorically. These instructions above all worked for me within the terminal session, but I could never get it to persist no matter what I did with export. Moreover, I couldn't find the .zshrc anywhere.
Turns out Apple does it differently. The file you need to edit is etc/paths. You can simply sudo nano /etc/paths and add your path in a new line. Then simply restart terminal and voila.
for me PATH=$PATH:/path/to/file/bin
then export PATH worked.
to check echo $PATH . other solutions are adding the path temporarily.
I'm on Monterey 12.4 and the only way I could change the path was using the helper function. Editing text files in nano did diddly squat
# append
path+=('/foo/bar/yourpath')
# export to sub-processes
export PATH
to verify your new directory has been added correctly, you can use
print -l $path
thanks to the fact that its type is known to be an array
how to append new plugin to zshrc file. I tried the below syntax but it replaced with new one.
sed -i -e 's/plugins=(.*)/plugins=(zsh-syntax-highlighting)/' ~/.zshrc
I want to add git, file, docker etc.
pl give me the corrected syntax.
KSK

MacOS Bash command line overlap

I am new to Bash and just set my own terminal theme. However, when I typed a long command, the command line overlapped in one line instead of wrapping into two. I looked up for solutions and solved the problem by making sure I added \[...\] when using non-printing characters in a bash prompt. Yet, when I move up and down my cursor on previous command line, the command line still overlaps like this:
katnano .bash_profile
while normally it should be:
katrinachan#katrina :~$ nano .bash_profile
My .bash_profile looks like this:
export PS1="\[\e[1;38;5;73m\u#\h\e[m\] :\[\e[1;38;5;214m\W\e[m\]\$ "
export CLICOLOR=1
export LSCOLORS=Cxfxcxdxbxegedabagacad
May I know what I am missing in my .bash_profile file? Thanks
[Solved]
Thank you! It works perfectly. Now my code is:
export PS1="[\e[1;38;5;73m]\u#\h[\e[m] :[\e[1;38;5;214m]\W[\e[m]\$ "
export CLICOLOR=1
export LSCOLORS=Cxfxcxdxbxegedabagacad
The overlap problem doesn't exist anymore.
The \[...\] should only bracket sequences which do not involve cursor movement on output. You have the \u#\h\e sequence inside these brackets where obviously it should be outside.

ZSH aliases not found

When I open my ~/.zshrc file and add alias homestead=“cd ~/Homestead”, I expect to be able to type homestead and be taken to the Homestead folder.
Instead I get the following error:
zsh: command not found: “cd
Even when I use single quotes, i.e. alias homestead='cd ~/Homestead' and run source ~/.zshrc I get the same error.
UPDATE: Also, when I run which homestead I get homestead: aliased to "cd
How can I fix this?
The answer was to open ~/.zshrc in Sublime Text as opposed to TextEdit and to check that the " were coming up as 042 in an octal dump.
You don't need to define this alias at all in zsh. Add the following to your .zshrc:
setopt autocd
cdpath+=(~)
The first allows you to treat a directory name as a command, which implicitly sets the working directory of the current shell to the named directory. The second specifies that if the current directory doesn't have a directory whose name is used with cd (or by itself with autocd set), then try to find it in a directory named in the cdpath parameter.
With these two, simply typing Homestead will first try to run a command named Homestead; failing that, it tries to cd to ./Homestead, and failing that, will finally succeed in cding to ~/Homestead.
The double quotes must be ASCII, not Unicode outside the ASCII range. Load the file in your editor, disable any automatic mangling of single quotes and double quotes. Then replace the funny quotes with ASCII quotes " (code decimal 34, hex 22, octal 042). Or type the command at a prompt, then cut & paste it in your editor. If all else fails, add the alias at the end of your .zshrc with
printf 'alias homestead="cd ~/homestead"' >> ~/.zshrc
Verify the result with octal dump,
od -bc .zshrc
The number above the quotes should appear as 042.
Maybe your locale settings is auto correcting a double quote " into a localized double quote “ as you posted. Since this is not recognized as a valid quote in shell, a simple white-space would break the string. So the actual alias is “cd.
As to why alias homestead='cd ~/Homestead' does not work, it seems you changed the alias in ~/.zshrc. From the which homestead result, it can be seen that alias homestead='cd ~/Homestead' does not really work. Maybe there is another line of alias homestead=“cd ~/Homestead” hidden in .zshrc after it.
Just saying that for me what fixed it was an error in the first alias in my list that had a question mark in it.
Just switched to Mac OS Catalina and ~/.bashrc to ~/.zshrc and I guess zsh doesn't support question marks.
Maybe it'll help someone coming here from Google search like I did.

Unix commands not working on terminal

I'm not able to execute any unix commands on my Mac OS Terminal. I was adding a path to my Scala Installation Location.
You've broken your $PATH environment variable, by the looks of it in ~/.bash_profile.
To fix:
Start Terminal.app (with error message as you posted).
Manually set $PATH:
export PATH=/usr/bin:/bin
Edit your ~/.bash_profile:
vi ~/.bash_profile
Remove the ?? characters from the scala path. That looks like it might be an invisible character, so I would recommend deleting the line and retyping it.
Save and test.
It sounds like you've replaced your path, rather than appended the path to Scala. You'll now need to use the full path to commands and then you'll be able to restore the path.
You can call vi from /usr/bin/vim

How to suppress (or customize) Mac Terminal shell prompt

Currently in my Terminal, every shell prompt looks like ComputerName: FooDir UserName$. The UserName part simply wastes too much space out of my precious 80 columns. Is there a way to suppress it?
The prompt is defined by the environment variable PS1 which you can define in .bash_profile.
To edit it, open or create the (hidden) file .bash_profile:
nano .bash_profile
and add a line that says
export PS1=""
Between the quotation marks, you can insert what you would like as your terminal prompt. You can also use variables there:
\d – date
\t – time
\h – hostname
\# – command number
\u – username
\W – current directory (e.g.: Desktop)
\w – current directory path (e.g.: /Users/Admin/Desktop)
The default prompt for common Linux distributions would be \w $, which evaluates to ~ $ in your home directory or e.g. /Users $ somewhere else. There are also website (like this one) that can help you with building your prompt.
If you want to remove the UserName part, your choice would be \h: \w$.
Once you made your changes, save the file with Control+o, Return, Control+x.
Here's an excellent article with a full list of Variables and Colors:
Customize your Shell Command Prompt
For a simple, minimalistic prompt, you can try this. Add the following line to your .bash_profile or simply test it first by running it in your terminal:
export PS1="\[\033[0m\]\w\$ "
It'll look something like this:
Here's my Prompt (source), also very simple:
export PS1="\[\033[1;97m\]\u: \[\033[1;94m\]\w \[\033[1;97m\]\$\[\033[0m\] "
2019 onwards, MacOS default shell is Z Shell. To customize command prompt, add a file named .zshrc in user home and put following line that sets a PS1 environment variable with desired prompt format:
export PS1="[%n]%~> "
Open new terminal
This is result of following format expansion:
%n User name
%~ Current directory
See full list of available expansions here.
Your answer can be found right here:http://www.hypexr.org/bash_tutorial.php#vi at about the middle of the page. :)

Resources