0

I'm new to shell scripting and was reading online how shell scripts could be written.

I made this following script but it doesn't seem to work, but when I type this command directly in the terminal window then it works. Can someone please tell me why this is not working.

Thanks in advance.. Following is my script code

#!/bin/bash
history | grep " ls$" | cut -c 1-5 > /tmp/histDelTemp

But when I type

history | grep " ls$" | cut -c 1-5 > /tmp/histDelTemp

directly in the Terminal then it works correctly and generates the output in the /tmp/histDelTemp file.

Mohit Tomar
  • 160
  • 1
  • 11
  • yo run the script file and does not it give any output or you could not run the script – Yusuf Şengün Jan 09 '21 at 08:51
  • 3
    bash disables history tracking in non-interactive shells, and anyway the shell running the script would have a different history than the interactive shell you're running this from. Use a function instead of a script. This is a possible duplicate of [`history -s` in a script doesn't work](https://stackoverflow.com/questions/8116052/history-s-in-a-script-doesnt-work) – Gordon Davisson Jan 09 '21 at 08:53

0 Answers0