2

I have standalone PROMPT_COMMAND= in my .bash_profile. Every time user input a command, it's being run. I want to get the first word of the last command by user.

For example, if the user runs: $ printf "Hey!", I want to access printf from my PROMPT_COMMAND.

$0 gives me -bash and $1, $2, ... gives me arguments as said on the site below. https://wiki.bash-hackers.org/scripting/posparams#the_first_argument

But how can I access the first word like printf in the above example?

Preferably, I want to use the build-in commands as much and cleanly as possible.

Penguinlay
  • 37
  • 7
  • https://stackoverflow.com/questions/14177700/copy-current-command-at-bash-prompt-to-clipboard has some hints but nothing which really solves this problem. – tripleee Jan 23 '19 at 04:57

1 Answers1

2

You could use fc:

$ printf "%s %s\n" "foo" "bar"
foo bar
$ fc -ln -1
       printf "%s %s\n" "foo" "bar"

That prints the entire command with arguments and weird spacing, so I suppose you could do:

$ fc -ln -1 | awk '{ print $1 }'
printf

EDIT: In the case you don't want pressing Return to come back as fc you could do:

$ fc -ln -1 | awk '$1 !~ /fc/ {print $1}' 

To create a function in ~/.bash_profile:

# Show last command without args
lcm () { fc -ln -1 | awk '$1 !~ /lcm/ {print $1}' ; }

Then for example:

$ date
Wed Jan 23 16:29:14 MST 2019
$ lcm
date

GNU : Bash History Built-ins

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    Nice trick. But, it won't work when the user's last input is nothing (entering return key) though. – Penguinlay Jan 23 '19 at 23:02
  • @I'L'l, I've been trying it out. I am seeing two different behaviors and somehow it's not working yet. When I use it inside `PROMPT_COMMAND`, it prints out the last command as expected but still it's the case as long as user doesn't enter on nothing. When I call just the function, it prints out the last command, not `lcm` if the last command is not empty. Otherwise, it prints out `lcm`. – Penguinlay Jan 23 '19 at 23:45
  • Actually, calling function also works the same as in `PROMPT_COMMAND` as described above. Still printing the last command if the user enter nothing. My comment above was wrong as I called lcm after lcm followed by entering nothing a few times. – Penguinlay Jan 23 '19 at 23:54
  • Did you reload (source) your `~/.bash_profile` after making the changes? `. ~/.bash_profile`, also are wanting to save the command into a variable? – l'L'l Jan 23 '19 at 23:56
  • I did reload `~/.bash_profile`. I have two functions, one is where I put the command output to variable like `input=$(fc -ln -1 | awk '$1 !~ /fc/ {print $1}')`. Another function is just lcm function that you posted above. Both of them give me the same result. – Penguinlay Jan 23 '19 at 23:59
  • Yes, I see. That's a tricky problem. `stdin` appears to ignore the return entered directly after a command for whatever reason and picks up whatever command was before it. I'm not exactly sure why it behaves this way, although maybe there is a way around it somehow; I would need to dive a bit deeper into it though. – l'L'l Jan 24 '19 at 00:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187216/discussion-between-penguinlay-and-lll). – Penguinlay Jan 24 '19 at 00:23