Questions tagged [whitespace]

combination of spaces, tabs, or newlines which create "empty" space in a line or column

Filter by
Sorted by
Tagged with
268
votes
4answers
210k views

Why does my shell script choke on whitespace or other special characters?

Or, an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
133
votes
9answers
278k views

How do I trim leading and trailing whitespace from each line of some output?

I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into? Example file: test space at back test ...
96
votes
3answers
34k views

Why does Vim indent pasted code incorrectly?

In Vim, if I paste this script: #!/bin/sh VAR=1 while ((VAR < 10)) do echo "VAR1 is now $VAR" ((VAR = VAR +2)) done echo "finish" I get these strange results: #!/bin/...
46
votes
1answer
9k views

Why is bash not storing commands that start with spaces?

If I perform a sequence of commands like: $ ls $ grep abc file.txt and then use the up arrow key to get the previous one, the terminal will show the last cmd (which is the grep here) But if I do ...
26
votes
1answer
25k views

Ignore whitespaces changes in all git commands

I've found tons of sites that explain how to have git warn you when you're changing line endings, or miscellaneous other techniques to prevent you from messing up an entire file. Assume it's too late ...
24
votes
2answers
73k views

Removing all spaces, tabs, newlines, etc from a variable?

This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel). I am getting spaces in that variable. + 0 != ...
20
votes
3answers
14k views

How to automatically strip trailing spaces on save in Vi and Vim?

Is there a .vimrc setting to automatically remove trailing whitespace when saving a file? Ideally (to be safe) I would like to only have this functionality for certain files, e.g. *.rb
14
votes
3answers
5k views

Brackets in if condition: why am I getting syntax errors without whitespace?

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"] &&...
14
votes
2answers
4k views

POSIX compliant way to work with a list of filenames possibly with whitespace

I have seen Bash scripting guides suggesting the use of array for working with filenames containing whitespace. DashAsBinSh however suggests that arrays are not portable so I am looking for a POSIX ...
13
votes
3answers
5k views

Why does bash have a HISTCONTROL=ignorespace option?

Or to phrase it differently, why would you want to prevent commands from being written to the bash history? (Inspired by a related question.)
12
votes
3answers
2k views

What difference does it make matching a word with/without a trailing whitespace?

I am learning shell-scripting and for that I am using HackerRank. There is a question related to sed on the same site: 'Sed' command #1: For each line in a given input file, transform the first ...
12
votes
4answers
1k views

bash: whitespace-safe procedural use of find into select

Given these file names: $ ls -1 file file name otherfile bash itself does perfectly fine with embedded whitespace: $ for file in *; do echo "$file"; done file file name otherfile $ select file in *;...
11
votes
2answers
4k views

Quoted vs unquoted string expansion

for i in $(xrandr); do echo "$i" ; done for i in "$(xrandr)"; do echo "$i"; done for i in "$(xrandr)"; do echo $i; done I understand why 1 differs from 2. But why does 3 give a different output from ...
10
votes
4answers
3k views

Why does cut fail with bash and not zsh?

I create a file with tab-delimited fields. echo foo$'\t'bar$'\t'baz$'\n'foo$'\t'bar$'\t'baz > input I have the following script named zsh.sh #!/usr/bin/env zsh while read line; do <<&...
8
votes
3answers
2k views

How to print strings separated by TAB in bash?

I am trying to print two string separated by a TAB. I have tried: echo -e 'foo\tbar' printf '%s\t%s\n' foo bar Both of them print: foo bar Where the whitespace between the two is actually 5 ...
7
votes
2answers
4k views

Why are bash tests so picky about whitespace?

As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third? #works if [...
7
votes
5answers
8k views

How to make xargs handle spaces and special chars from cat?

I have a file that contains a list of names. i.e.: Long Name One (001) Long Name Two (201) Long Name Three (123) ... with spaces and some special characters. I wanted to make directories out of ...
7
votes
2answers
13k views

Strip trailing whitespace from files

The answer to removing trailing whitespace with sed has most of the answer, but I want sed -i 's/[ \t]*$//' $1 to be able to take arbitrary number of file arguments as a shell script on the command ...
7
votes
7answers
5k views

Trailing spaces when copying from console

It's this annoying behavior I've been experiencing here and there occasionally: when you select text with mouse in console (that is, copy it), paste it, and realize you've got extra spaces at the end ...
6
votes
5answers
3k views

Replace any number of tabs and spaces with single new line in Linux? [closed]

I have a very large word list that has been damaged, maybe by improper vim exit or memory exhaustion? Everything in my word list should be left-justified. But now it is like this: Cat ...
6
votes
2answers
795 views

Bash commands with spaces in them (git config or git-config?)

Please forgive me if this seems easy, but I only started learning Unix 2 days ago. Basically, I have been taught that when typing a command into the terminal it needs to be of the form: [command ...
6
votes
1answer
500 views

How to compile Linux kernel modules within white-space-separated directories under the following makefile?

I was trying to compile a kernel module source code, until I noticed that some whitespaces where causing mismatched path names. The directory under which I found myself was: axor@vacuum:~/software/CS ...
5
votes
10answers
2k views

How to quickly add some space in the terminal?

I often find myself scrolling back through the terminal with the mouse-wheel to see the first of many C++ template errors issued by a command. Anticipating this, I'll hold down the Return key to give ...
5
votes
1answer
879 views

Replace each tab ONLY at the beginning of each line with spaces

So replacing all tabs in a file with spaces is not hard. In vim for example, I can do %s/\t/ /gc And if I want to replace the ones at the beginning of each line, not the one in the middle I can ...
5
votes
1answer
3k views

Rename files with spaces in a BusyBox shell

I'm trying to rename some files on my Synology Diskstation via SSH. The available shell is the BusyBox built-in shell: BusyBox v1.16.1 (2013-04-16 20:13:10 CST) built-in shell (ash) The move command ...
4
votes
1answer
5k views

Octals 302 240 together seem to correspond to non-breaking space

By looking at a particular line of a text file (say, the 1123th, see below), it seems that there is a non-breaking space, but I am not sure: $ cat myfile.csv | sed -n 1123p | cut -f2 Lisztes feher $ ...
4
votes
2answers
3k views

Using files that have spaces in their name in pipes [duplicate]

I have folder named play which contains a file damn file (with space). When I use find and pipe the output to tar: find play/ -name 'damn*' | tar cf archive.tar -T - It works perfectly. Why does ...
4
votes
1answer
3k views

terminal command that prints out tabs, spaces, and newlines as \t \s and \n

I wrote a terminal program in c that converts tabs to spaces. And I would like to see that it works and how many spaces were converted from tabs. The problem is when I create a file called input.txt ...
4
votes
2answers
824 views

In Vim, how can I automatically determine whether to use spaces or tabs for indentation?

Sometimes I edit others source code where the prevailing style is to use tabs. In this case, I want to keep the existing convention of using literal tabs. For files I create myself, and files that ...
4
votes
1answer
6k views

Storing whitespace in a shell script variable [duplicate]

I need to store specific number of spaces in a variable. I have tried this: i=0 result="" space() { n=$1 i=0 while [[ "$i" != $n ]] do result="$result " ((i+=1)) ...
4
votes
2answers
274 views

Maintain filenames as separate arguments in successive commands

A disclaimer: This isn't related to one specific command like ls or grep, although I use them in the examples, this is more a bash thing. Let's say I want to see the file sizes, timestamps, and ...
4
votes
1answer
413 views

Trailing spaces when copying from gnome-terminal using zsh

When copying text from gnome-terminal with zsh trailing whitespace is also copied. For example copying the following clearly adds a vertical scollbar on stackexchange site as trailing whitespace the ...
4
votes
2answers
575 views

Generic Preprocessor adds extra whitespace

Following up on this article I use GPP to empower Markdown parser pandoc with some macros. Unfortunately, gpp seems to copy all whitespace into the result. For example, consider file test.md % Title ...
3
votes
1answer
3k views

Posix Character Sets difference between [[:blank:]] and [[:space:]]?

On this website it says: [[:blank:]] space and tab characters [[:space:]] whitespace characters What's the difference between space and tab characters and whitespace characters? To me, they ...
3
votes
1answer
756 views

Indented here document causes a syntax error

I have the following code fragment in my shell script and have a bug that I can't seem to pin down a reason for despite some research. When I run the code as below the script exits with a message ...
3
votes
3answers
9k views

How to identify and remove invisible whitespace characters on gedit?

I want to remove unnecessary whitespace on my css file. I am using grep with the command as follows: $ grep -rn "[[:space:]]$" Surprisingly, it is returning a hit on every line in the file. I search ...
3
votes
2answers
5k views

How can I count the number of whitespace characters in a file?

I wanted to count the number of whitespace characters in a file. The best I could come up with was: tr -cd [:space:] < my_file | wc Is there a neater way?
3
votes
2answers
29 views

Remove trailing whitespace in df command

I have the following df command: > df -h --output=used,avail,pcent / Used Avail Use% 5.9G 12G 34% Notice how there is a space before Used and 5.9GB So I did the following: > df -h --...
3
votes
1answer
1k views

how to get less to show tabs as tabs?

When I view data with tabs in less, tab characters are expanded to spaces. If I then copy them directly out of my terminal using ctrl-c, they copy as spaces. On the other hand, more preserves tab ...
3
votes
1answer
312 views

Zsh preexec - How to trim all leading indentations from a codeblock?

Say I copy the following codeblock from a .md handbook which incldes some information and a few codeblocks (hence it's not a script file): ⇨⇨⇨⇨( ⇨⇨⇨⇨Indented commands... ⇨⇨⇨⇨) The Zsh preexec ...
3
votes
3answers
2k views

What perl script will scp directory with spaces in name?

I"ve been trying for several hours to figure out how to get perl to scp a directory with a space in the name. I'm currently getting the error scp: ambiguous target Here is the script that is not ...
3
votes
2answers
13k views

what is “an invisible whitespace character that takes up space”

One can look up Unicode Characters with Regular Expressions. On Jan Goyvaerts website I found a RegExp whose meaning I don't understand : \p{Zs} or \p{Space_Separator}: a whitespace character that ...
3
votes
1answer
1k views

How to use whitespace in procmailrc recipe destination path?

I use Mutt Maildir for storage mails. I created one mailbox like this Stack Exchange/. (whitespace between Stack and Exchange here) procmailrc recipe: :0 * From: .*Stack\ Exchange Stack Exchange/ ...
2
votes
2answers
6k views

How to add a string with spaces at the end of command output

I want to add a string with spaces at the end of each line of the output of ls command. #!/bin/sh STRING_WITH_SPACES="this should be at the end of output" ls -lh | sed 's|$| '$STRING_WITH_SPACES'|' ...
2
votes
6answers
12k views

How do I deal with directories that have spaces in their names from `find`? [duplicate]

I have this: #!/bin/bash for file in `find . -type d` do echo $file done If I have just one directory called My Directory, the output is My Directory How do I fix this? The echo $file is just ...
2
votes
3answers
1k views

How to sort filenames with spaces in them and concatenate the files?

I have the misfortune of dealing with filenames that contain spaces. I want to concatenate files of which filenames contain spaces. I also want to sort the filenames numerically. Obviously the ...
2
votes
4answers
144 views

Ignore spacing between echo strings

I use echo to print two strings into a file in either of these two commands: echo -e "\n""x" >> ~/file echo -e "\n" "x" >> ~/file The first result is: x The second result is: x I ...
2
votes
1answer
4k views

Binary operator expected - quoting question for shell script args

I have a question on how I'm adding arguments when executing a shell script. I have a simple script which helps me block ip ranges: ~/block_ip.sh: if [ ! $3 ] then echo "usage ~/block_ip.sh (DROP/...
2
votes
3answers
1k views

Process a file list with space in file names [duplicate]

I am trying develop a bash script which processes a list of files which may contain space names. (I have already consulted Trouble in script with spaces in filename and Why I can't escape spaces ...
2
votes
1answer
598 views

Change to a directory containing a space

I'm using a MacBook with OS X installed. I'm on Terminal trying to go to a directory. So I'm in Home and I use the command ls and there is a VirtualBox VMs directory. But when I try to do cd ...