How to you configure the command prompt in Linux to show current directory?
You can place this to your .zshrc
file
export PS1="%d %% "
%d
denotes the CWD
For more details go here for example
How to show/hide current working directory in bash prompt?
You should look up Bash prompt special characters.
As you can see in the list, the current working directory is \w
, and \a
is the bell character, so just remove those.
How can I shortern my command line prompt's current directory?
Consider this script using awk instead of sed for your case:
pwd_length=14
pwd_symbol="..."
newPWD="${PWD/#$HOME/~}"
if [ $(echo -n $newPWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD=$(echo -n $newPWD | awk -F '/' '{
print $1 "/" $2 "/.../" $(NF-1) "/" $(NF)}')
fi
PS1='${newPWD}$ '
For your example of directory ~/workspace/projects/project1/folder1/test
it makes PS1 as: ~/workspace/.../folder1/test
UPDATE
Above solution will set your prompt but as you noted in your comment that it will NOT change PS1 dynamically when you change directory. So here is the solution that will dynamically set PS1 when you change directories around.
Put these 2 lines in your .bashrc file:
export MYPS='$(echo -n "${PWD/#$HOME/~}" | awk -F "/" '"'"'{
if (length($0) > 14) { if (NF>4) print $1 "/" $2 "/.../" $(NF-1) "/" $NF;
else if (NF>3) print $1 "/" $2 "/.../" $NF;
else print $1 "/.../" $NF; }
else print $0;}'"'"')'
PS1='$(eval "echo ${MYPS}")$ '
if (NF > 4 && length($0) > 14)
condition in awk will only apply special handling when your current directory is more than 3 directories deep AND if length of $PWD
is more than 14 characters otherwise and it will keep PS1 as $PWD
.
eg: if current directory is ~/workspace/projects/project1$
then PS1 will be ~/workspace/projects/project1$
Effect of above in .bashrc will be as follows on your PS1:
~$ cd ~/workspace/projects/project1/folder1/test
~/workspace/.../folder1/test$ cd ..
~/workspace/.../project1/folder1$ cd ..
~/workspace/.../project1$ cd ..
~/.../projects$ cd ..
~/workspace$ cd ..
~$
Notice how prompt is changing when I change directories. Let me know if this is not what you wanted.
Linux prompt with current path displayed
A shell variable known as PS1
is responsible for displaying the default command prompt in bash. To see your current setup try echo "$PS1"
. You can set PS1
in your .bashrc file for a custom prompt. It sounds like you want something like this:
PS1="\H:\w$ "
Explanation:
\H
- The hostname of the machine
\w
- The working directory, relative to $HOME (or ~
)
You can try different combinations if you want something even more customized. For a reference, check out this blog: https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
Display the current directory name in bash terminal window
Since $PWD
is guaranteed to be a directory, you might use either of the following:
PROMPT_COMMAND='echo -ne "\033]0;$$ ${BRANCH} $(basename "$PWD") \007"'
PROMPT_COMMAND='echo -ne "\033]0;$$ ${BRANCH} ${PWD##*/} \007"'
How do I show the real current directory in my bash prompt, not the symlink
Use $(pwd -P)
instead of \w
. Like this:
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(pwd -P)\[\033[00m\]$ '
How to custom display prompt in KornShell to show hostname and current directory?
From reading the ksh man page you want
PS1="${HOSTNAME}:\${PWD##*/} \$ "
Tested on default ksh on SunOS 5.8
How do I run a program with a different working directory from current, from Linux shell?
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c
, then executes helloworld
from /a
. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c
, make the execution of helloworld
conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld
via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
Related Topics
How to Accept Multiple Tcp Connections in Perl
Linux Service Can't Load Library Path in The /Etc/Ld.So.Conf.D
Identify Program That Connects to a Unix Domain Socket
Tools Required to Learn Arm on Linux X86 Platform
Execute a Script After Every Git Push
Do_Install Error While Running Custom Bitbake in Poky Build
What Is The Equivalent of /Proc/Cpuinfo on Freebsd V8.1
How to Clean Only Target in Buildroot
How to Idiomatically Package Dependencies for a Qt Application Using Cpack
Why Would I Use "Service Sshd Reload" in Preference to "Service Sshd Restart"
Debian Start Qt Gui Application with No Desktop
Linux: How to Lock The Pages of a Process in Memory
How to Block Push to Master Branch on Remote
Cannot Change The Maximum Open Files Per Process with Sysctl
Watermarking Video from The Linux Command Line
How to Access Raspberry Pi Qemu Vm via Network
File Format Differences Between a Static Library (.A) and a Shared Library (.So)