I'm trying to grab the last executed command, with options, and store it as a string to be written to a file. I've tried using fc
but that can only give me the next to last command (for me anyways) and I've also tried !!:p
which gives the right command when I type it but I can't seem to get it to work in the function itself. Can I accomplish this?
Here's what's going on. arg
is what I want the string to be.
#!/bin/bash
foo(){
read HISTNUMVAR < histnum.txt
TEMPHNV=$(fc -l 0 | grep -o '^[0-9]*');
if [ $TEMPHNV -ne $HISTNUMVAR ]; then
arg=$(***last history command here***);
./write_arg_to_file.sh $arg;
fi
echo $TEMPHNV > histnum.txt;
}
The script does everything else I need it to do besides grab the right history command.
Best Answer
There's probably a cleaner way, but you can get the previous command from history with