简体   繁体   中英

How to pass an bash command line argument to sed -n p?

I want to output a specific line from bash argument using sed , I have tried many ways, but none work:

#!/bin/bash
sed -n '$2p' $1 
sed -n '${2}p' $1 
sed -n "$2p" $1 
sed -n "${2}p" $1 
sed -n ''"$2"'p' $1

How on earth do I get the correct result?

Try

sed -n "$2p" $1

Demo:

$seq 10 > file.txt
$cat temp.ksh 
#!/bin/bash
set -x
sed -n "$2p" $1
$./temp.ksh  file.txt 3
+ sed -n 3p file.txt
3
$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM