Ubuntu – Pass variable value to script from terminal

bashcommand line

I like the shred command so instead of using the 'rm' command I prefer shred. I would like to create a bash script in /usr/bin, say we will call it "shredder" so that if I want to shred a file (i.e. test.txt) all I have to do is type:

shredder test.txt

And the bash script does it for me. I made a bash script like this but can't figure out how to pass it the fie name in terminal before I run it. Any help would be highly appreciated.

Here's my code.

#!/bin/bash
clear
read fle
shred -n 3 -zvfu $fle

Thanks in advance!

Best Answer

you can use script in following manner

#!/bin/bash
clear    
for var in "$@"
  do
    shred -n 3 -zvfu "$var"
  done

Or A shorter version

#!/bin/bash
clear    
for var
  do
    shred -n 3 -zvfu "$var"
  done

Run your script followed by the file name you want to shred. You can use multiple file separated by space.

Related Question