2

I want to edit she bang line #!/bin/bash line on the top of files in all the files with .sh extension how it done by scripting.

codaddict
  • 445,704
  • 82
  • 492
  • 529
sunil
  • 751
  • 1
  • 8
  • 9

3 Answers3

3

You can do this using the sed command:

sed -i "1i #!/bin/bash" *.sh
  • sed: name of the command
  • -i : sed option to edit the file inplace
  • 1 : line number
  • i : to do the insertion before the line number provided before
  • #!/bin/bash: the shebang to be added
  • *.sh: to make this work on all .sh files
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • its works for existing and newly created sh files is there any option that it impact only on newly created sh files not disturbing the previous ones – sunil Aug 06 '10 at 06:03
  • No, there is not such option for sed. You'll have to use **find** combined with this sed to modify only newly created files. – codaddict Aug 06 '10 at 06:05
  • can u please suggest how to use sed with find "by showing example" i mean is this possible by using ctime options or what – sunil Aug 06 '10 at 06:07
0
find /path -iname "*.sh" -type f -exec sed -i.bak '1i #!/bin/bash' "{}" +;
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

Here is an example of how this can be achieved:

#!/bin/bash
echo "New file name?"
read -r name
touch $name
echo "'$name' created"
cp shebang.txt "$name"
chmod +x "$name"

I use a simple text file containing only the shebang.

Andrey Dmitriev
  • 528
  • 2
  • 9
  • 27
Johnny
  • 1
  • 1