# sed and variable as target file

**URL:** https://community.unix.com/t/sed-and-variable-as-target-file/333907
**Category:** Shell Programming and Scripting
**Created:** [September 4, 2013, 1:00pm UTC](https://community.unix.com/t/sed-and-variable-as-target-file/333907 "2013-09-04T13:00:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jnojr](https://community.unix.com/user_avatar/community.unix.com/jnojr/32/2556_2.png) [@jnojr](https://community.unix.com/u/jnojr)
#### Post date: [September 4, 2013, 1:00pm UTC](https://community.unix.com/t/sed-and-variable-as-target-file/333907/1 "2013-09-04T13:00:28Z")

</div>

```nohighlight
for file in `ls /tmp/*_sw_list`; do
  /usr/bin/sed -i '' '1,/^Software\ Update/d' $file
done

```

In my script, this doesn't work. I can copy-and-paste it, and it works. Enabling debugging shows it is resolving the file name correctly... it isn't an issue with special characters in the filename. It returns an error code of 0, but doesn't actually modify the file. Permissions on the file are 644.

---

<div class="post-metadata">

### Author: ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)
#### Post date: [September 4, 2013, 1:03pm UTC](https://community.unix.com/t/sed-and-variable-as-target-file/333907/2 "2013-09-04T13:03:10Z")

</div>

This is a [useless use of ls \*](http://partmaps.org/era/unix/award.html#ls).

Piping the \* through ls accomplishes nothing you couldn't have accomplished without it, except possibly scrambling any filenames with whitespace in them -- backticks split across all whitespace, not newlines.

```plaintext
for FILE in /tmp/*_sw_list
do
        /usr/bin/sed -i '' '1,/^Software\ Update/d' "$FILE"
done

```

...or even:

```plaintext
/usr/bin/sed -i '' '1,/^Software\ Update/d' /tmp/*_sw_list

```

What does the '' do? I don't understand the meaning of the blank expression, but if you say it works...

Why are you escaping a space?

---

<div class="post-metadata">

### Author: ![jnojr](https://community.unix.com/user_avatar/community.unix.com/jnojr/32/2556_2.png) [@jnojr](https://community.unix.com/u/jnojr)
#### Post date: [September 4, 2013, 1:51pm UTC](https://community.unix.com/t/sed-and-variable-as-target-file/333907/3 "2013-09-04T13:51:00Z")

</div>

> [@corona688](#):
>
> This is a [useless use of ls \*](http://partmaps.org/era/unix/award.html#ls).

Above examples are for '\*', to catch EVERYTHING. I'm going to wind up with an unknown number of files in /tmp/ called file\_sw\_list, file2\_sw\_list, etc. etc.

> [@](#):
>
> Piping the \* through ls accomplishes nothing you couldn't have accomplished without it, except possibly scrambling any filenames with whitespace in them -- backticks split across all whitespace, not newlines.

It would be a pretty freaky coincidence to wind up with filenames matching my pattern with whitespace... they're generated from unquotes variables. It could happen, and I always like to sanity check as much as possible, but that's pretty far down my list of worries.

> [@](#):
>
> ```plaintext
> for FILE in /tmp/*_sw_list
> do
> /usr/bin/sed -i '' '1,/^Software\ Update/d' "$FILE"
> done
> 
> ```
> 
> ...or even:
> 
> ```plaintext
> /usr/bin/sed -i '' '1,/^Software\ Update/d' /tmp/*_sw_list
> 
> ```

Neither work. I get the same result.

> [@](#):
>
> What does the '' do? I don't understand the meaning of the blank expression, but if you say it works...

Two single quotes, not a double quote. I'm performing the operation on a file, and don't want a "backup". BSD sed is picky about this stuff (OS is OSX).

> [@](#):
>
> Why are you escaping a space?

Because there's a literal space in the pattern I'm matching?

In any case, it turned out to be a second pattern that matched '^Software\ Update' that was so close to the top I didn't notice just a couple of lines missing 🙂

---

<div class="post-metadata">

### Author: ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)
#### Post date: [September 4, 2013, 1:55pm UTC](https://community.unix.com/t/sed-and-variable-as-target-file/333907/4 "2013-09-04T13:55:37Z")

</div>

> [@jnojr](#):
>
> Above examples are for '\*', to catch EVERYTHING. I'm going to wind up with an unknown number of files in /tmp/ called file\_sw\_list, file2\_sw\_list, etc. etc.

Look closely, my examples use \* too. They are not pseudocode, \* does actually work in those places. That's why putting it through ls is redundant.

If you're concerned about \* matching too many files and using ls instead because of this, plugging too many files into ls does not make them not be too many files either.
