Total programming newbie, probably extremely naive - bear with me.

As part of a current assignment, we've been given the task of grouping specific commands into a shell script. (date, hostname, arch...etc... top, history), using VIM to type the script.

My script is extremely basic, makes liberal use of "echo " to create blank lines for easy readability. I've got almost everything working - except every time I run the shell script, it fails when it reaches the "history" command.

The segment of the script is:

echo Finally, here is the output of the history command
echo -------------THE END---------------------

But upon running the script (using "sh [filename].sh"), the output reads:

[filename].sh: 66: [filename].sh: history: not found

I figure 66 refers to the line of the text document/script.

Can anyone help me get this working? I feel like it should be so simple, but I've tried numerous variations and am met with this "not found" message every time.

Thanks for your time!

  • Thanks for your response! I'm using a Virtual Machine to perform this task. Outside of VIM, the "history" command works fine, shows all of the past commands entered.
    – FiascoFinn
    Mar 31, 2020 at 19:16
  • What do you mean exactly by "a VIM shell script"? VIM is an editor, not a shell (although it has the ability to run shell commands). Do you just mean that you are using VIM to write a script? if so, that's largely irrelevant - which shell are you writing the script for, and how are you executing it? Mar 31, 2020 at 19:22
  • Apologies - this is what I mean, I'm entirely new to this, I feel as though I don't know what I don't know! I'm using VIM to write the script. The shell is "bash" (to the best of my understanding?). I'm running the script file by typing "sh shellattempt2.sh" (my file's name/extension) in the main terminal window, outside of VIM. Typing "sh shellattempt2.sh" and hitting Enter outputs she script/text file with its correct formatting. Anywhere I've used a command (date, etc) in the file, the command executes. But for "history", I get an error message; no history is printed.
    – FiascoFinn
    Mar 31, 2020 at 19:31

2 Answers 2


By typing sh shellattempt2.sh, you are likely forcing the script to be interpreted using the /bin/sh shell interpreter - which, on Ubuntu, is dash not bash by default

The dash shell doesn't have history so that's why you are getting the error

[filename].sh: 66: [filename].sh: history: not found

Instead, make sure your script has an appropriate shebang as the first line


then make it executable (chmod +x [filename].sh) and then run it using


HOWEVER, bash only enables history by default in interactive shells. The simplest way to access the shell history from a script (which runs in its own non-interactive shell) is probably to look directly at the history file itself, ex.

  • My typing "sh shellattempt2.sh" may be down to my inexperience - I'm quite sure I'm working with bash. Typing "echo $0" produces a "-bash" as output, so I think that means I'm working with the bash shell? I've added the #!/bin/bash at the start of my script. Now, the error message is gone, but at the "history" section of the script, it seems to just skip it. So I'm inclined to think, whatever "history" is being called, is blank? (when I substituted "history" with "cat $HISTFILE", the script seemed to break at that point).
    – FiascoFinn
    Mar 31, 2020 at 20:03

In addition to steeldriver's answer, to output history of current bash session in case if you've added #!/bin/bash, you could use source function:

source shellattempt2.sh

Because of with #!/bin/bash added there's no history output in current bash session.

If you replace history by cat $HISTFILE or cat "$HISTFILE" also use source. But it outputs not current bash session's history.

source --help
source: source filename [arguments]
    Execute commands from a file in the current shell.

    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.

    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
  • 1
    Okay - by following what you said, my script worked as I intended it to. Thanks!
    – FiascoFinn
    Mar 31, 2020 at 20:25

You must log in to answer this question.

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