2

I am customizing my Bash prompt by adding more information, which contains a glance to the list of current directory.

I already understand that changing variable PS1 would help me with everything. Even more, this variable allows me to execute commands each time.

I've tried to use ls --color=always to get a list of files in the current directory. The implementation is below:

PS1='... $(ls -F --color=always | xargs | cut -d " " -f -20)\n ...'

The shell gets me up to 20 file names in the current directory each time. However, it never tells me if there are more files. What do I need to do? I expect it to tell me with the ellipsis ('...') or something else.

PS: 'ls' seems to behave differently according to the output environment (e.g. tty and file). What if I want a formatted output in the prompt (with more lines)?

Robson
  • 813
  • 5
  • 21
  • 40
Daniel Shao
  • 137
  • 1
  • 8
  • @biffen: `xargs` without any argument is same as `xargs echo`. Basically, OP wants to flatten out all the adjacent white-spaces into a single space character. – anishsane Apr 08 '19 at 08:36

2 Answers2

1

If you just want to see the list of files in the current directory before every prompt, you can just set PROMPT_COMMAND=ls. Don't play with PS1 for such purpose.

For your requirement, I think this would work:

PROMPT_COMMAND="ls | awk 'NR>20{print \"...\"; exit}1' | xargs"

Explanation:

  1. PROMPT_COMMAND=something: Runs something before showing the PS1 prompt.
  2. ls: list files. Feel free to add -F or --color=auto
  3. awk 'NR>20{print "..."; exit}1': If line number is greater than 20, just print ... and exit. Else, print line as is (that trailing 1).
  4. xargs: To flatten the white-spaces into single space character.

Note: awk | xargs can be merged into a single awk command, thereby saving you a process for every prompt. However, that would need extra escaping of " character, which can get confusing for beginners.

PROMPT_COMMAND="ls --color=always | awk 'NR>20{printf \"...\"; exit}{printf \$0 \" \";}END{print \"\"}'"
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • My appreciation to your introduction of `PROMPT_COMMAND`. I am now wondering why there needs to be an `END` in `awk` command. Does the `print ""` sentence only mean a new line before the prompt? – Daniel Shao Apr 08 '19 at 11:45
  • Earlier `printf` statements do not add the newline `\n`. Explicit `print ""` or `printf "\n"` would add it. – anishsane Apr 09 '19 at 04:30
0

Maybe you can just use

$(ls -F --color=always | xargs | cut -d " " -f -20; [ $(ls -l . | egrep -c '^-') -gt 20 ] && echo ...)

The second part is from Count number of files within a directory in Linux?.

Yet I suppose there is a better way (e.g. not calling ls twice).

Hope it helps.

fennecdjay
  • 56
  • 1
  • 6
  • Thanks for your suggestion. I think it possible not to call `ls` twice. Maybe storing the result in a variable shall work, and of course this process needs to be finished in a local function. I'll try it. – Daniel Shao Apr 08 '19 at 11:29
  • Great. Please let me now what you find. – fennecdjay Apr 08 '19 at 12:47
  • I've tried as following: First define a function named `showdir()`. Then define a local variable `local FILES=$(ls --color=always -F)` and show it by `echo ${FILES} | xargs | cut -d " " -f -20; [ $(wc -w <<< "${FILES}") -gt 20 ] && echo " ..."`. In `PS1` or `PROMPT_COMMAND`, just do like this: `... $(showdir) ...`. It seems awkward but OK in this way. I do not know the trade-off exactly. – Daniel Shao Apr 09 '19 at 05:24
  • Thank You. I'll give it a look! – fennecdjay Apr 09 '19 at 09:09