Skip to main content
added 99 characters in body
Source Link

Final answer

cat <<< "prepended text
$(cat test.txt)" > test.txt

Context

I wasn't too satisfied with the answers as they felt like too much typing. I liked John Alberts his answer but couldn't stand to type -e. Unfortunately, I accidentally read over John Alberts his echo 2 liner as well (significantly reducing the value of this answer and me 30 minutes playing around, but oh well, it happens).

In any case, I was focused on finding something that meant you only needed to type the filename and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).

I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.

I wasn't too satisfied with the answers as they felt like too much typing.

I was focused on finding something that meant you only needed to type the filename and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).

I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.

Final answer

cat <<< "prepended text
$(cat test.txt)" > test.txt

Context

I wasn't too satisfied with the answers as they felt like too much typing. I liked John Alberts his answer but couldn't stand to type -e. Unfortunately, I accidentally read over John Alberts his echo 2 liner as well (significantly reducing the value of this answer and me 30 minutes playing around, but oh well, it happens).

In any case, I was focused on finding something that meant you only needed to type the filename and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).

I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.

deleted 1 character in body
Source Link

I wasn't too satisfied with the answers as they felt like too much typing. 

I was focused on finding something that meant you only needed to type the file namefilename and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).

I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file)$(cat file) can be replaced by the equivalent but faster $(< file)$(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.

I wasn't too satisfied with the answers as they felt like too much typing. I was focused on finding something that meant you only needed to type the file name and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings. I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice.

I wasn't too satisfied with the answers as they felt like too much typing. 

I was focused on finding something that meant you only needed to type the filename and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings since in the right context they reduce cognitive strain (i.e. typing < 3 times doesn't require too much thinking power).

I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice and is more well-known than the trick of the Bash Reference Manual.

Source Link

I wasn't too satisfied with the answers as they felt like too much typing. I was focused on finding something that meant you only needed to type the file name and text you want to prepend.

Moreover, I was searching for something that looked aesthetically intuitive. With that I mean: the preprend needs to physically show, even if it'd be an illusion it'd have the effect of a mnemonic.

So I tried an approach with herestrings. I created a file test.txt with the word "monkeys".

And I typed:

cat <<< "prepend
> $(< test.txt)"

Output:

prepend
monkeys

A bit of clarification:

You need to manually press enter yourself.

On the second line the > is from the shell itself, you don't need to type that.

Notes:

(1) What I couldn't manage was a one liner. There seems to be no herestring combination in which I could use $() and \n. Which is why you need to press the newline manually yourself.

(2) $(< test.txt) has the same effect as $(cat test.txt). The Bash Reference Manual states:

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

So you could also do:

cat <<< "prepend
> $(cat test.txt)"

More typing, but I admit a bit less cognitive strain since cat is being typed twice.