zsh prompt uses variables names (despite unsetting AUTO_NAME_DIRS) - terminal

I have assigned a directory to a variable in my .zshrc file like so:
export DOTFILES=$HOME/.dotfiles
Now my zsh prompt reads ~DOTFILES when I am in the .dotfiles directory. I did some research (in this thread: Variable names in prompt instead of path) and found that zsh has an AUTO_NAME_DIRS option which does this very thing.
I tried unsetopt AUTO_NAME_DIRS but my zsh prompt still shows the variable name instead of the path. I ran the unsetopt command and found that autonamedirs is indeed in the list of unset options. I also ran setopt to make sure it wasn't in the list of set options, and it is not.
Any ideas on how to fix this?

Think of it as a hook to create named directories that is triggered at variable setting.
So if auto_name_dirs parameter is set and you set some value % TMP=/tmp, it will also create a named dir for /tmp using TMP. If you later unset the parameter, the directory name remains. If you create a number of variables % USER=/usr with auto_name_dirs unset, and after set the option, no directory naming will take place.
~ % zsh -f
dhcp-193-107% setopt autonamedirs
dhcp-193-107% TMP=/tmp
dhcp-193-107% export PS1="dir-prompt %~ : "
dir-prompt ~ : cd /tmp
dir-prompt ~TMP : unset autonamedirs
dir-prompt ~TMP : cd /
dir-prompt / : cd /tmp
dir-prompt ~TMP : # see the dir name still exists
dir-prompt ~TMP : USER=/usr
dir-prompt ~TMP : cd /usr
dir-prompt /usr : # no dir name for /usr
dir-prompt /usr : setopt autonamedirs
dir-prompt /usr : cd /
dir-prompt / : cd /usr
dir-prompt /usr : # still no dir name for /usr

Related

'sh' environment does not respect the PATH extensions, user's local PATH not in effect?

$ bl 1
$ sh -c 'bl 1'
sh: bl: command not found
The bl script is located in the user's PATH extension (/home/user/.local/bin) but the sh environment does not seem to be aware of it, the bash is. The main /usr/bin/sh executable symlinks to /usr/bin/bash.
Placing a symlink in /usr/local/bin pointing to the local bl script does seem to fix the issue. Expanding the PATH manually $ PATH=/usr/bin:$HOME/.local/bin sh -c 'bl 1' also solves it, something I don't really understand since both the bash and the sh are aware of the PATH.
$ export -p |grep PATH=
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"
$ sh -c 'export -p |grep PATH'
export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"
"Something's missing and you have to find it" yet it's hard to look if you don't know what is missing.
$ export -p |grep PATH
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"
Having a literal ~ is wrong. It should have been expanded to /home/user already. The shell will expand ~ when variables are assigned but not when they're expanded.
$ foo=~ && echo $foo # expanded at assignment
/home/user
$ foo='~' && echo $foo # not expanded since the assignment is quoted
~
Find the shell startup script where ~/.local/bin is added to the $PATH and make sure ~ is not quoted.
Wrong:
PATH="$PATH:~/.local/bin"
Right:
PATH=$PATH:~/.local/bin

`cat ../test` does not find a file. How to force `cat` to not resolve symlink?

I create this file as:
$ echo "ABC" > /home/kes/test
I can cd to home and out of it:
kes#work ~/s $ cd ..
kes#work ~ $ cat test
ABC
kes#work ~ $ cd s
kes#work ~/s $
~/s is soft link:
kes#work ~/s $ pwd
/home/kes/s
kes#work ~/s $ pwd -P
/home/kes/work/projects/safevpn/repo2
But when I use relative path it does not work:
kes#work ~/s $ cat ../test
cat: ../test: No such file or directory
But while
I do not expect that cat try to open file in parent directory (resolves symlink) because cd does not resolve it:
$ rm /home/kes/test
$ mkdir /home/kes/test
$ cd ~/s
$ cd ../test
$ pwd
/home/kes/test
How to make these commands work consistent?
If you read the man page of cd, you will see that cd can or cannot resolve the soft-links, depends on which option do you give (-L or -P). If no option was given, cd uses default -L, which means, not resolving soft links.
If both −L and −P options are specified, the last of these options
shall be used
and all others ignored. If neither −L nor −P is specified, the operand shall be
handled dot-dot logically;
And
−L Handle the operand dot-dot logically; symbolic link
components shall
not be resolved before dot-dot components are processed (see steps 8.
and 9. in the DESCRIPTION).

How can I cd using a variable in bash

I have the following script:
#!/bin/bash
set -o errexit # Exit on error
# Enable script to run from anywhere
root="$(dirname ${BASH_SOURCE[0]})"
cd "$($root)"
source ./scripts/main
cd "$($root)"
pwd
source ./scripts/test
cd "$($root)/applicant"
yarn build
But I get this error:
./build.sh: line 8: .: filename argument required
How can I get a variable of the current directory?
you can use pwd for current directory.
-61T9:~ pwd
/Users/test
-61T9:~ s=`pwd`
-61T9:~ echo $s
/Users/test
You can do it simply:
cd $variable_with_path
In your example you should replace "$($root)" by $root
To get variable with current directory:
path=`pwd`

Cygwin .bashrc PATH gets duplicate entries

I've got the following variable set in my cygwin $HOME/.bashrc
PATH=/bin:/usr/sbin:"/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin":$PATH
Problem is that when I login and the .bashrc gets executed, I get starting duplicates as follows:
Dragos#dragos ~
$ echo $PATH | tr ':' '\n'
/bin
/usr/sbin
/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
/bin
/usr/sbin
/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
/usr/local/bin
/usr/bin
/cygdrive/c/WINDOWS
/cygdrive/c/WINDOWS/system32
/cygdrive/c/WINDOWS/System32/Wbem
/cygdrive/c/curl
/
/cygdrive/c/gnupg
/cygdrive/c/Progra~1/cvsnt
/cygdrive/c/Progra~1/GNU/WinCvs 2.0
/cygdrive/c/Progra~1/Notepad++
/cygdrive/c/Progra~1/PuTTY
/cygdrive/c/Progra~1/WinSCP
/cygdrive/c/Python26
/cygdrive/c/Python26/Lib/site-packages/PyQt4/bin
/cygdrive/c/Python26/Scripts
/usr/bin
/usr/lib/lapack
Does anyone know what causes this?
Here's my .bashrc
$ cat ~/.bashrc
# base-files version 3.7-1
# To pick up the latest recommended .bashrc content,
# look in /etc/defaults/etc/skel/.bashrc
# Modifying /etc/skel/.bashrc directly will prevent
# setup from updating it.
# The copy in your home directory (~/.bashrc) is yours, please
# feel free to customise it to create a shell
# environment to your liking. If you feel a change
# would be benificial to all, please feel free to send
# a patch to the cygwin mailing list.
# User dependent .bashrc file
# Shell Options
# #############
# See man bash for more options...
# Don't wait for job termination notification
# set -o notify
# Don't use ^D to exit
# set -o ignoreeof
# Use case-insensitive filename globbing
# shopt -s nocaseglob
# Make bash append rather than overwrite the history on disk
# shopt -s histappend
# When changing directory small typos can be ignored by bash
# for example, cd /vr/lgo/apaache would find /var/log/apache
# shopt -s cdspell
# Completion options
# ##################
# These completion tuning parameters change the default behavior of bash_completion:
# Define to access remotely checked-out files over passwordless ssh for CVS
# COMP_CVS_REMOTE=1
# Define to avoid stripping description in --option=description of './configure --help'
# COMP_CONFIGURE_HINTS=1
# Define to avoid flattening internal contents of tar files
# COMP_TAR_INTERNAL_PATHS=1
# If this shell is interactive, turn on programmable completion enhancements.
# Any completions you add in ~/.bash_completion are sourced last.
# case $- in
# *i*) [[ -f /etc/bash_completion ]] && . /etc/bash_completion ;;
# esac
# History Options
# ###############
# Don't put duplicate lines in the history.
# export HISTCONTROL="ignoredups"
# Ignore some controlling instructions
# export HISTIGNORE="[ ]*:&:bg:fg:exit"
# Whenever displaying the prompt, write the previous line to disk
# export PROMPT_COMMAND="history -a"
# Aliases
# #######
# Some example alias instructions
# If these are enabled they will be used instead of any instructions
# they may mask. For example, alias rm='rm -i' will mask the rm
# application. To override the alias instruction use a \ before, ie
# \rm will call the real rm not the alias.
# Interactive operation...
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
# Default to human readable figures
# alias df='df -h'
# alias du='du -h'
# Misc :)
# alias less='less -r' # raw control characters
# alias whence='type -a' # where, of a sort
# alias grep='grep --color' # show differences in colour
# Some shortcuts for different directory listings
alias ls='ls -hF --color=tty' # classify files in colour
# alias dir='ls --color=auto --format=vertical'
# alias vdir='ls --color=auto --format=long'
# alias ll='ls -l' # long list
# alias la='ls -A' # all but . and ..
# alias l='ls -CF' #
# Functions
# #########
# Some example functions
# function settitle() { echo -ne "\e]2;$#\a\e]1;$#\a"; }
# Notepad++ function
# Pass in a UNIX path
# Starts notepad++ given a UNIX path argument
function notepadpp() {
local notepadUnixPath="/cygdrive/c/Program Files/Notepad++/notepad++.exe"
#local notepadArgPath=$(eval $(echo cygpath -w -a "$*"))
local notepadArgPath=`cygpath -w -a "$*"`
"$notepadUnixPath" -multiInst "$notepadArgPath" &
}
alias notepad++=notepadpp
# Explorer function
# Pass in a UNIX path
# Starts explorer given a UNIX path argument
function explorer() {
local explorerArgPath=`cygpath -w -a "$*"`
cmd /C start "" "$explorerArgPath" &
}
alias vi=vim
# Change filename starting with prefix string to another prefix string
alias mvprefix='$HOME/mvprefix.sh'
# Change filename ending with suffix string to another suffix string
alias mvsuffix='$HOME/mvsuffix.sh'
# Change filename ending with suffix string to a string prefixed with todays date
alias todaysuffix='$HOME/todaysuffix.sh'
# Generate secure passwords by default
alias pwgen='pwgen -y -c -s -n'
export INPUTRC=$HOME/.inputrc
export EDITOR=vim
export PATH=/bin:/usr/sbin:"/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin":$PATH
# Overwrite DOS env variable APPDATA with our own for installing perl CPANPLUS
export APPDATA=$HOME
Here's my .bash_profile
$ cat .bash_profile
# base-files version 3.7-1
# To pick up the latest recommended .bash_profile content,
# look in /etc/defaults/etc/skel/.bash_profile
# Modifying /etc/skel/.bash_profile directly will prevent
# setup from updating it.
# The copy in your home directory (~/.bash_profile) is yours, please
# feel free to customise it to create a shell
# environment to your liking. If you feel a change
# would be benifitial to all, please feel free to send
# a patch to the cygwin mailing list.
# ~/.bash_profile: executed by bash for login shells.
# source the system wide bashrc if it exists
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi
# source the users bashrc if it exists
if [ -e "${HOME}/.bashrc" ] ; then
source "${HOME}/.bashrc"
fi
# Set PATH so it includes user's private bin if it exists
# if [ -d "${HOME}/bin" ] ; then
# PATH=${HOME}/bin:${PATH}
# fi
# Set MANPATH so it includes users' private man if it exists
# if [ -d "${HOME}/man" ]; then
# MANPATH=${HOME}/man:${MANPATH}
# fi
# Set INFOPATH so it includes users' private info if it exists
# if [ -d "${HOME}/info" ]; then
# INFOPATH=${HOME}/info:${INFOPATH}
# fi
Here's my /etc/bash.bashrc
$ cat /etc/bash.bashrc
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any warranty.
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software.
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
# base-files version 4.1-1
# /etc/bash.bashrc: executed by bash(1) for interactive shells.
# The latest version as installed by the Cygwin Setup program can
# always be found at /etc/defaults/etc/bash.bashrc
# Modifying /etc/bash.bashrc directly will prevent
# setup from updating it.
# System-wide bashrc file
# Check that we haven't already been sourced.
([[ -z ${CYG_SYS_BASHRC} ]] && CYG_SYS_BASHRC="1") || return
# If not running interactively, don't do anything
[[ "$-" != *i* ]] && return
# Set a default prompt of: user#host and current_directory
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u#\h \[\e[33m\]\w\[\e[0m\]\n\$ '
# Uncomment to use the terminal colours set in DIR_COLORS
# eval "$(dircolors -b /etc/DIR_COLORS)"
I don't modify $PATH in my $HOME/.bashrc anywhere other than the set PATH= command above.
If I prepend only one path to $PATH, that would get duplicated as well:
PATH="/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin":$PATH
Results in:
Dragos#dragos ~
$ echo $PATH | tr ':' '\n'
/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
/usr/local/bin
/usr/bin
/cygdrive/c/WINDOWS
/cygdrive/c/WINDOWS/system32
/cygdrive/c/WINDOWS/System32/Wbem
/cygdrive/c/curl
/
/cygdrive/c/gnupg
/cygdrive/c/Progra~1/cvsnt
/cygdrive/c/Progra~1/GNU/WinCvs 2.0
/cygdrive/c/Progra~1/Notepad++
/cygdrive/c/Progra~1/PuTTY
/cygdrive/c/Progra~1/WinSCP
/cygdrive/c/Python26
/cygdrive/c/Python26/Lib/site-packages/PyQt4/bin
/cygdrive/c/Python26/Scripts
/usr/bin
/usr/lib/lapack
So... Why the duplicates?
Addendum:
I found that I'm executing bash twice in my C:\cygwin\Cygwin.bat
The reason is that I have a context menu command to "Open Bash Here" that passes
a starting path to C:\cygwin\Cygwin.bat
Here's my C:\cygwin\Cygwin.bat
#echo off
C:
set PATH=%PATH%;C:\cygwin\bin
REM SHELL needed for any screen instances started from bash
set SHELL=/bin/bash
set HOME=C:\cygwin\home\Dragos
set HOMEDRIVE=C:
set HOMEPATH=\cygwin\home\Dragos
REM
if not [%1]==[] (
C:\cygwin\bin\cygpath %1 > tmpFile
set /p startingpath= < tmpFile
del tmpFile
)
if "%startingpath%"=="" start C:\cygwin\bin\mintty.exe --icon /Cygwin-Terminal.ico --size 140,50 --exec /bin/bash --login -c "exec /bin/bash -rcfile ~/.bashrc"
if not "%startingpath%"=="" start C:\cygwin\bin\mintty.exe --icon /Cygwin-Terminal.ico --size 140,50 --exec /bin/bash --login -c "cd '%startingpath%'; exec /bin/bash -rcfile ~/.bashrc"
exit
Addendum:
Figured out that I need to pass --noprofile --norc to bash when calling bash.
Here's the updated C:\cygwin\Cygwin.bat
#echo off
C:
set PATH=%PATH%;C:\cygwin\bin
REM SHELL needed for any screen instances started from bash
set SHELL=/bin/bash
set HOME=C:\cygwin\home\Dragos
set HOMEDRIVE=C:
set HOMEPATH=\cygwin\home\Dragos
REM
if not [%1]==[] (
C:\cygwin\bin\cygpath %1 > tmpFile
set /p startingpath= < tmpFile
del tmpFile
)
if "%startingpath%"=="" start C:\cygwin\bin\mintty.exe --icon /Cygwin-Terminal.ico --size 140,50 --exec /bin/bash --login
if not "%startingpath%"=="" start C:\cygwin\bin\mintty.exe --icon /Cygwin-Terminal.ico --size 140,50 --exec /bin/bash --noprofile --norc --login -c "cd '%startingpath%'; exec /bin/bash -rcfile ~/.bashrc"
exit
I don't have Cygwin installed, and I don't have a Windows machine, so I can't give you a boatload of details.
See if the man bash page can help you. In normal BASH, the /etc/profile, /etc/bashrc, the $HOME/.bash_profile, the $HOME/.bashrc, and sometimes the $HOME/.profile are all read in depending whether this is a login shell or not. Cygwin has it's own special versions of each of these files in the /etc directory. However, there's also other scripts that get invoked and can affect your Cygwin environment. For example, there are special scripts to import Windows environment variables including %PATH%.
In Cygwin, the default is to include the Windows %PATH% variable as part of the Cygwin path. It's actually a general import of all Windows environment variables (and depending upon the installation, the \ is sometimes converted to a / and short directory names are used).
If you open xterm windows and not standard Windows console windows for your Cygwin command line, you'll also have to check the xserve script (or whatever it's called) because that also imports a lot of stuff into the Cygwin environment.
I've used Cygwin in the past, and every time I use Cygwin, I find myself chasing down these exact things, plus a few other issues: For example, the default Kornshell load environment script has a bug in it. I believe they have a literal "^G" instead of a Ctrl-G, or maybe it was another control character. I can't remember. All I know is I spend about an hour or two cleaning up my Cygwin environment every time I install it. I like Cygwin, but it can be a pain.
Sorry I can't give you more specific directions.
Just set the PATH to whatever you like (without a reference to $PATH). You shouldn't trust the PATH that some random sysadmin thinks is a good PATH anyway. Do this in the file sourced last for your shell.

How can I set the current working directory to the directory of the script in Bash?

I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in.
The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.
#!/bin/bash
cd "$(dirname "$0")"
The following also works:
cd "${0%/*}"
The syntax is thoroughly described in this StackOverflow answer.
Try the following simple one-liners:
For all UNIX/OSX/Linux
dir="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
Bash
dir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
Note: A double dash (--) is used in commands to signify the end of command options, so files containing dashes or other special characters won't break the command.
Note: In Bash, use ${BASH_SOURCE[0]} in favor of $0, otherwise the path can break when sourcing it (source/.).
*For Linux, Mac and other BSD:
cd "$(dirname "$(realpath -- "$0")")";
Note: realpath should be installed in the most popular Linux distribution by default (like Ubuntu), but in some it can be missing, so you have to install it.
Note: If you're using Bash, use ${BASH_SOURCE[0]} in favor of $0, otherwise the path can break when sourcing it (source/.).
Otherwise you could try something like that (it will use the first existing tool):
cd "$(dirname "$(readlink -f -- "$0" || realpath -- "$0")")"
For Linux specific:
cd "$(dirname "$(readlink -f -- "$0")")"
*Using GNU readlink on BSD/Mac:
cd "$(dirname "$(greadlink -f -- "$0")")"
Note: You need to have coreutils installed
(e.g. 1. Install Homebrew, 2. brew install coreutils).
In bash
In bash you can use Parameter Expansions to achieve that, like:
cd "${0%/*}"
but it doesn't work if the script is run from the same directory.
Alternatively you can define the following function in bash:
realpath () {
[[ "$1" = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
This function takes 1 argument. If argument has already absolute path, print it as it is, otherwise print $PWD variable + filename argument (without ./ prefix).
or here is the version taken from Debian .bashrc file:
function realpath()
{
f=$#
if [ -d "$f" ]; then
base=""
dir="$f"
else
base="/$(basename -- "$f")"
dir="$(dirname -- "$f")"
fi
dir="$(cd -- "$dir" && /bin/pwd)"
echo "$dir$base"
}
Related:
How to detect the current directory in which I run my shell script?
How do I get the directory where a Bash script is located from within the script itself?
Bash script absolute path with OS X
Reliable way for a Bash script to get the full path to itself
See also:
How can I get the behavior of GNU's readlink -f on a Mac?
cd "$(dirname "${BASH_SOURCE[0]}")"
It's easy. It works.
The accepted answer works well for scripts that have not been symlinked elsewhere, such as into $PATH.
#!/bin/bash
cd "$(dirname "$0")"
However if the script is run via a symlink,
ln -sv ~/project/script.sh ~/bin/;
~/bin/script.sh
This will cd into the ~/bin/ directory and not the ~/project/ directory, which will probably break your script if the purpose of the cd is to include dependencies relative to ~/project/
The symlink safe answer is below:
#!/bin/bash
cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" # cd current directory
readlink -f is required to resolve the absolute path of the potentially symlinked file.
The quotes are required to support filepaths that could potentially contain whitespace (bad practice, but its not safe to assume this won't be the case)
This script seems to work for me:
#!/bin/bash
mypath=`realpath $0`
cd `dirname $mypath`
pwd
The pwd command line echoes the location of the script as the current working directory no matter where I run it from.
There are a lot of correct answers in here, but one that tends to be more useful for me (making sure a script's relative paths remain predictable/work) is to use pushd/popd:
pushd "$(dirname ${BASH_SOURCE:0})"
trap popd EXIT
# ./xyz, etc...
This will push the source file's directory on to a navigation stack, thereby changing the working directory, but then, when the script exits (for whatever reason, including failure), the trap will run popd, restoring the current working directory before it was executed. If the script were to cd and then fail, your terminal could be left in an unpredictable state after the execution ends - the trap prevents this.
I take this and it works.
#!/bin/bash
cd "$(dirname "$0")"
CUR_DIR=$(pwd)
Get the real path to your script
if [ -L $0 ] ; then
ME=$(readlink $0)
else
ME=$0
fi
DIR=$(dirname $ME)
(This is answer to the same my question here: Get the name of the directory where a script is executed)
cd "`dirname $(readlink -f ${0})`"
Most answers either don't handle files which are symlinked via a relative path, aren't one-liners or don't handle BSD (Mac). A solution which does all three is:
HERE=$(cd "$(dirname "$BASH_SOURCE")"; cd -P "$(dirname "$(readlink "$BASH_SOURCE" || echo .)")"; pwd)
First, cd to bash's conception of the script's directory. Then readlink the file to see if it is a symlink (relative or otherwise), and if so, cd to that directory. If not, cd to the current directory (necessary to keep things a one-liner). Then echo the current directory via pwd.
You could add -- to the arguments of cd and readlink to avoid issues of directories named like options, but I don't bother for most purposes.
You can see the full explanation with illustrations here:
https://www.binaryphile.com/bash/2020/01/12/determining-the-location-of-your-script-in-bash.html
echo $PWD
PWD is an environment variable.
If you just need to print present working directory then you can follow this.
$ vim test
#!/bin/bash
pwd
:wq to save the test file.
Give execute permission:
chmod u+x test
Then execute the script by ./test then you can see the present working directory.

Resources