I am adding some rules to an application. I want to make a bash script which would take my input, for example: 'abc', add a prefix 'pqr' and a suffix 'xyz' to the input and send it as a command.

In this case the command to be sent would be pqrabcxyz without any spaces as I didn't add them.

The script I am using is

read -p "ID to be banned: " cmd
"iptables --append INPUT --match string --algo kmp --string '${cmd}' --jump DROP -p udp"
  • Sample of real input (is it form a file? How are you getting this input?) and expect result would help the most. Any code you have tried will also let us know what your skill level is really like too.
    – Tigger
    Dec 6, 2017 at 6:00

2 Answers 2

read -p "Please type in your command" cmd

Based on the limited information in the question, this should be enough. I hope that @ayush-mishra can be more specific.

Answer edited thanks to reminder by @grgarside

  • The Script I am Using is: #!/bin/bash read -p "ID to be banned: " cmd "iptables --append INPUT --match string --algo kmp --string '${cmd}' --jump DROP -p udp" Dec 6, 2017 at 7:10
  • But It is Not working. It Shows no command found Dec 6, 2017 at 7:10
  • You have to be root to run iptables. Switch to root first with su or use sudo in a proper way so that iptables is invoked as root. You may also need to change iptables to /sbin/iptables Dec 6, 2017 at 7:55
  • Its not working Dec 6, 2017 at 8:54
  • @Ayush Don't use "…" around the iptables command.
    – grg
    Jan 6, 2018 at 14:14

You can try with below command


echo "abc" | sed "s/^/pqr/g" | sed "s/$/xyz/g"


echo "abc" | sed "s/^/"$j"/g" | sed "s/$/"$z"/g"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .