I have some commands in txt file and I need to execute all them line by line. How could I do it?
            Asked
            
        
        
            Active
            
        
            Viewed 7.6k times
        
    75
            
            
        - 
                    15How about `bash file.txt`? – Vaughn Cato Nov 26 '12 at 15:31
- 
                    1possible duplicate of [ubuntu run text file as command](http://stackoverflow.com/questions/9825495/ubuntu-run-text-file-as-command) – mido Jun 26 '15 at 09:35
- 
                    You may also have to convert it to UNIX format with `dos2unix file.txt` – understorey May 10 '22 at 09:12
4 Answers
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
- 
                    This works with native commands but I have an *alias* stored in my file and this way not working. I got error: "No command 'mycm' found ...". Are there another way for it? – Hank Phung Mar 08 '17 at 11:05
- 
                    1
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
 
     
    