75

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

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Андрей
  • 873
  • 1
  • 7
  • 5

4 Answers4

127

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.

Hank Phung
  • 2,059
  • 1
  • 23
  • 38
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
27

In a Terminal just type:

bash path/to/my/file.txt

And you will get each lines executed.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
-2

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

Debobroto Das
  • 834
  • 6
  • 16
  • If who made the question doesn't how to execute commands stored in a file, I doubt he will able to rename or change permission without any extra explanation. – Rubens Mariuzzo Nov 26 '12 at 15:38
  • If he needs executing command, then obviously he knows something about the commnadline(though I don't expect he knows very mush about shell). I think working in shell is a immense beauty and nice. I told him about the very basics of shell programming. In stackoverflow, some coding is always been wellcomed. Then If I say him about the coding in shell, then what is wrong?? and why the dwnvote? – Debobroto Das Nov 27 '12 at 02:28
-5

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

Echilon
  • 10,064
  • 33
  • 131
  • 217
Cradam
  • 15