Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: run script starting from a specific line

Tags:

bash

I there a way to run a bash script starting from a specific line?

Assume my script looks like that:

0 #!/bin/bash
1 ...
2 clone a repository
3 ...
4 command that crashed

I go and fix line 4 and I want to rerun the script without running the code from lines 1,2,3 but starting directly from line 4.

I quick search for the title of this question didn't give me an answer. If there is a way to do that with bash it would be useful.

Thank you for the answers. Another solution if you also want to keep some lines in the beginning is:

 # assuming you want to run your script except from line 4 to 7
 sed '4,7s/^/#/g' -i script.sh; ./sript.sh #this replaces the beginning of the line ^ with # from lines 4 to 7
 # to undo the comments
 sed '4,7s/^#//g' -i script.sh; ./sript.sh
like image 799
Daniela Avatar asked Feb 11 '23 16:02

Daniela


1 Answers

Try this:

 bash <(sed -n '5,$p' script.sh)
like image 129
Cyrus Avatar answered Feb 23 '23 15:02

Cyrus