Run bash commands from txt file

LinuxFileBash

Linux Problem Overview


I have some commands in txt file and I need to execute all them line by line. How could I do it?

Linux Solutions


Solution 1 - Linux

Just do bash file:

$ cat file 
date
echo '12*12' | bc

$ bash file
Mon Nov 26 15:34:00 GMT 2012
144

In case of aliases just run bash -i file

No need to worry about file extensions or execution rights.

Solution 2 - Linux

In a Terminal just type:

bash path/to/my/file.txt

And you will get each lines executed.

Solution 3 - Linux

just change the extension of the file to .sh

add /bin/bash at starting of the file

change the permission of the file to executable

than simply run ./filename.sh command in command line. all the commands in the file will be executed

Solution 4 - Linux

Change the extension to .sh and for the first line #!/usr/bin/env bash and as someone else said chmod +x

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionАндрейView Question on Stackoverflow
Solution 1 - LinuxChris SeymourView Answer on Stackoverflow
Solution 2 - LinuxRubens MariuzzoView Answer on Stackoverflow
Solution 3 - LinuxDebobroto DasView Answer on Stackoverflow
Solution 4 - LinuxCradamView Answer on Stackoverflow