Assuming you're talking about this guy in CSL's Bash shell...
[CSLID@royal-26] (1)$ echo hello CSL
$PS1 ^^^^^^^^^^^^^^^^^^^^^
hello CSL
There is \w and \W:
[CSLID@royal-26] (1)$ PS1='\h:\w\$ '
royal-26:~/cs400/P105$ PS1='\h:\W\$ ' royal-26:P105$
GNU Bash Manual has a
comprehensive list of such expansions. Actually, since we are using
Git, and you're asking about prompt, I have to promote
git-prompt.sh here. (Sorry :P Many people have been using
zsh and P10K yet completely overlooking this tiny shell script... I
think it deserves to be known better.)
Now normally this lives in /usr/share/git/completion,
however I cannot find it on the CSL machine (and they don't have plocate
installed, sad).... but we can get one from Git's contrib folder
ourselves!
royal-26:P105$ mkdir ~/.bashrc.d
royal-26:P105$ curl -L https://raw.githubusercontent.com/git/git/refs/heads/master/contrib/completion/git-prompt.sh > ~/.bashrc.d/git-prompt.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21338 100 21338 0 0 743k 0 --:--:-- --:--:-- --:--:-- 771k
royal-26:P105$ source ~/.bashrc.d/git-prompt.sh royal-26:P105$ PS1='\h:\W$(__git_ps1)\$ ' royal-26:P105 (main)$
Sweet! Now we can move $(__git_ps1) wherever we like...
and oh to do this at login, literally just copy everything I ran after
curl (now conveniently separated into its own code block)
into your ~/.bashrc. I believe you can do it... (There are
options too in git-prompt.sh in case the prompt becomes too
slow: read the comments at the top of the file... it is self-documenting
:)
By the way, command number is not "useless". They are for re-running past commands (so-called History Expansions). Though I can't quite get it to work, so I'll use (t)csh to demonstrate (learned this after the poor guy had tcsh as their login shell... their conundrum still makes me laugh to this day @137):
royal-26:~/cs400/P103> set prompt = %m:%~[%h]%#
royal-26:~/cs400/P103[9]> make && make test [runs test]
royal-26:~/cs400/P103[10]> !9 [runs test, ok...]
royal-26:~/cs400/P103[11]> !9 [still runs test!]
royal-26:~/cs400/P103[12]>
I feel like Bash should work in the same way, otherwise the CSL
admins wouldn't have put it there, but the only number I could get
working is those in history and negative numbers (like
!-n to run the n-th latest command)... What I do know works
in Bash is !! which repeats the last command (actually, it
turns out to be a shorthand of !-1). So if you ever:
$ pacman -Syu error: you cannot perform this operation unless you are root.
forget to type sudo, then well uh...
$ sudo !! sudo pacman -Syu [sudo] password for [REDACTED]: ****** Your mother was a hamster and your father smelt of elderberries!
Oops, sorry. That was my /etc/sudoers acting up...
[sudo] password for [REDACTED]: ******* :: Synchronizing package databases...
Yay. It works :) (plus totally free of saying naughty words into your shell!)
So !! works, but... \# might actually be
sort of "useless" if you can't use !n like you can in
(t)csh... I dunno. Apparently I don't know enough about Bash to justify
its existence.
Edit! Turns out history expansion does work in Bash
(I just remembered this legendary
website's existence.) But you need a slightly different PS1
variable than \#: that is, \!:
\!
The history number of this command.
\#
The command number of this command.
You'll notice the similarity with tcsh's %!:
prompt The string which is printed before reading each
command from the terminal. prompt may include
any of the following formatting sequences (+),
which are replaced by the given information:
%h, %!, !
The current history event number.
Now, let's look at CSL's default Bash prompt:
[CSLID@vm-instunix-11] (1)$ echo $PS1 [\u@\h] (\#)$
Ew, gross. They didn't even bother to use \$ (which will
change into # if you are root/uid=0---this is basic PS1
knowledge!) Now let me demonstrate how to really get a
meaningful number:
[CSLID@vm-instunix-11] (2)$ PS1='[\u@\h \w] (\!)\$ ' [CSLID@vm-instunix-11 ~] (503)$ cd cs400/P103
[CSLID@vm-instunix-11 ~/cs400/P103] (504)$ make && make test make: Nothing to be done for `build'. [test ok x1] [CSLID@vm-instunix-11 ~/cs400/P103] (505)$ !504 make && make test [test ok x2] [CSLID@vm-instunix-11 ~/cs400/P103] (506)$ !504 make && make test [test ok x3]
Now it works just like csh! Actually, you can make it shorter. Maybe
swap \w out for \W, or remove the
\u that prints your CSLID on every line... I am showing
this because I've had something like this in my bash shell for a while
now, though I know it gets a little crowded when I'm, say, 13
directories deep into some git repository. But um... I'm fine with that
:] (so far!)