Rikg09
Rikg09

Reputation: 195

Using variables in sed - not sure if it is working

I am creating a variable, and a part of how it is created involved sed -n. The problem I'm having is that I'm not sure if it is working.

y=$(ls -Ap | grep "/$" | sed -n "$ip") is the first line and creates y.

But when I run

echo $y I simply get no output and a blank space. So I'm not sure if the sed is working or not, and if not how to fix it.

Thanks in advance.

Upvotes: 0

Views: 44

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

As User 123 mentioned, the problem is that bash tries to expand $ip instead of $i. You can use curly braces around the variable name to avoid that, like this ${i}.

However, I don't suggest to use the pipe solution since:

I suggest to use find: It can be done with a single invocation of find:

find -mindepth 1 -maxdepth 1 -type d -name "*${i}*"

Make sure that ${i} is set and not empty! Otherwise -name would expand to -name "**" which would match anything. You can do that using parameter substitution syntax:

find -mindepth 1 -maxdepth 1 -type d -name "*${i:-nonexistent}*"

Make sure that nonexistent does not exist! ;)

Upvotes: 3

Related Questions