1

Hi all I am having problems trying to embed this command in my bash prompt so that it will update itself each time the prompt is shown. I would like it to be part of the prompt itself

free -m | tr -s ' ' '\t' | grep Mem: | cut -f3

Suggestions and ideas would be appreciated

1
  • Do you want it as part of the prompt, or do you just want it run each time the prompt is shown? Please edit your question and clarify.
    – terdon
    May 26, 2016 at 17:14

2 Answers 2

4

What did you try? You could define a function in your ~/.bashrc, thusly:

function showmem() {
    free -m | tr -s ' ' '\t' | grep Mem: | cut -f3
}  

and then set Bash's PROMPT_COMMAND to that function

PROMPT_COMMAND=showmem

Remember to source ~/.bashrc whenever you change ~/.bashrc.

2
  • +1 but why the function? Why not simply PROMPT_COMMAND="free -m | tr -s ' ' '\t' | grep Mem: | cut -f3" ?
    – terdon
    May 26, 2016 at 17:13
  • I tried this PS1='\u@\h\n $(free -m | tr -s ' ' '\t' | grep Mem: | cut -f3) \$' in the command prompt to see if it worked but it threw this error t | grep Mem: | cut -f3) \$: command not found. If possible, I would like to be able to embed the command in the PS1 value in the ~/.bashrc. May 26, 2016 at 17:28
1

Instead of setting PROMPT_COMMAND, as @waltinator suggested, you could embed the command into the actual prompt with process substitution:

PS1='\u@\h\n$(showmem) \$ '

This PS1 is based on the one you mentioned in a comment, and uses the function that @waltinator made.

2
  • hi @wjandrea where would you suggest that I define the function showmen May 28, 2016 at 9:04
  • In the .bashrc right before the PS1 definition, but it doesn't really matter.
    – wjandrea
    May 28, 2016 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .