User: Password:
|
|
Subscribe / Log in / New account

Removing the quote distinction?

Removing the quote distinction?

Posted May 19, 2005 11:41 UTC (Thu) by liljencrantz (guest, #28458)
In reply to: Removing the quote distinction? by kleptog
Parent article: Fish - The friendly interactive shell

Hmm... I've never written scripts that do a huge amount of mixed string and variable output, so I never thought about the issues you describe. The way things work now, you'd either have to write:

echo "Couldn't open file: "$file

and

echo "Looking for /home/"$USER/$filename

which are _exactly_ the same length as the bash equivalent, but might feel unintuitive, or you could write:

printf "Couldn't open file: %s\n" $file

and

printf "Looking for /home/%s/%s\n" $USER $filename

which in my opinion look nice and clear, but they are slightly longer. It seems to me that for any complex formating, the power and clarity of printf far outweighs the brevity of echo and variables in quoted strings. But I might be wrong. You are more than welcome to try and persuade me that I am wrong.


(Log in to post comments)

Removing the quote distinction?

Posted May 19, 2005 12:20 UTC (Thu) by kleptog (subscriber, #1183) [Link]

Very true. I'm not really complaining about the length of the script, it just violates the (for me anyway) the principle of least surprise. I'm so used to other scripting languages, I know it is something that would trip me up continuously.

That said though, you've implemented several quite good ideas and I do hope some of them get implemented in other shells. I note zsh also has lots of nice features but bash has an installed base that would make IE proud.

Anyway, printf the right solution, but you've got a way ahead of you yet.

PS. How do you handle $@ ?

Removing the quote distinction?

Posted May 19, 2005 12:57 UTC (Thu) by liljencrantz (guest, #28458) [Link]

Besides shellscripts, what other scripting languages make the same distingction between single and double quotes?

As to $@, that is an excellent question! Arrays are one of the things I chose not to take up in this article. The article is rather long as it is already... The answer is that $@ is replaced by a general purpose array syntax.

The arguments to a function or program are stored in the 'argv' array variable. In fish, you access array elements with square brackets, so $argv[1] is the first array element. $argv expands to a list of all elements. So to loop over the arguments to a function or program, you can just write:

for i in $argv
[...]
end

You can also do sublists by writing $argv[1 2 5] or $argv[(seq 2 7)]. The latter example uses a subshell inside an array subscript.

Assigning array variables is just as easy as assigning regular variables, and uses the same command:

set colors red breen glue

sets the variable 'colors' to have tha values 'red', 'breen' and 'glue'.

You can alter one element of an array using:

set colors[2] green

to change the above array to 'red', 'green' and 'glue'

There is one last noteworthy thing about fish style arrays. Colon separated values found in the environment on startup are converted to arrays. So PATH , CDPATH and all the other colon-separated lists are arrays in fish. They are of course converted back into colon separated lists for all programs that fish spawns. The reason why fish hardcodes the colon character here instead of allowing the user to specify a separator character like IFS is that by hardcoding colon, it is possible to make sure that the values of variables are not altered by fish. It would be unfortunate if fish accidentally exported a tab-separated PATH to any programs it starts.

Removing the quote distinction?

Posted May 19, 2005 14:38 UTC (Thu) by Dabuk (subscriber, #1507) [Link]

Both perl and PHP differentiate between double and single quotes.

Removing the quote distinction?

Posted May 19, 2005 15:16 UTC (Thu) by kleptog (subscriber, #1183) [Link]

As pointed out, PHP and Perl differentiate.

Non-scripting languages like C, C++ and such almost always differentiate. SQL distinguishes.

Ah, Python doesn't appear to distinguish, perhaps this is how you got the idea. Python doesn't use $ to denote variables, so they don't need the distinction.

Removing the quote distinction?

Posted May 19, 2005 16:07 UTC (Thu) by liljencrantz (guest, #28458) [Link]

Actually I mostly got the idea from another language I occasionally misuse: English. It is common in written English to nest quotations within quotations by alternating between single and double quotes. It seems intuitive and useful to me, as I often end up writing strings that will be parsed at a later time. I thought that this would be unsurprising for other people as well, but it seems I might have been wrong on that one. So I may have the law of least surprise against me on this one.

On the other hand, one reason for not wanting two kinds of quotes is that I feel the constructs of computer languages should be as orthogonal as possible. This way you maximize the number of ideas that can be concicely expressed with a syntax of a given size. Two different types of quotes that do nearly the same thing does not strike me as an elegant solution.

Removing the quote distinction?

Posted May 19, 2005 17:44 UTC (Thu) by smeg4brains (guest, #207) [Link]

Yeah.. the language seems pretty nice, but I do have to agree with people that the quotes should be handled differently..

Maybe keep the difference in the quotes as usual, but steal python's
''' , '' , '
and
""" , "" , "
ideas.

All of the single quote variations could skip the variable expansion, but all of the double quote versions could expand variables..

This way you can keep some of the backward compatibility with bash/sh, but you also have cleaner/better quote mechanisms as well..

Example:

echo """Hello $USER

Welcome to "Ackerman.umci.com".
All connections logged."""

echo '''To add to your $PATH variable, use something like:
export PATH="$PATH:/home/jeffb/.bin/" '''

Removing the quote distinction?

Posted May 19, 2005 23:20 UTC (Thu) by liljencrantz (guest, #28458) [Link]

Yes, using something like Pythons one, two or three quotes is an option. It means that quotes can be nested, while also allowing different kinds of quotes.

I think I'll wait for a while and see if the screams for more quoting options die out. I'd rater keep the language small. Also, I'm kind of lazy.

Removing the quote distinction?

Posted Mar 9, 2008 20:30 UTC (Sun) by Mysingen (guest, #50982) [Link]

Why not just escape the variables instead?

set a Hello
set b '\$a is $a'
set a Goodbye
echo $b

should print "$a is Hello"

It is just a hassle to keep track of what will be expanded in bash scripts and I would really
like the distinction between ' and " to go away.

Removing the quote distinction?

Posted May 20, 2005 18:43 UTC (Fri) by mikeraz (guest, #155) [Link]

fish is "friendly" and for beginners printf syntax isn't.

I'd feel more comfortable with variable expansion in quoted strings so that

% set file ~/mail/sent
% echo "File is set to $file"
File is set to ~/mail/sent

to echo $file you escape it
% echo "File is set to \$file"
File is set to $file

Removing the quote distinction?

Posted May 21, 2005 10:30 UTC (Sat) by liljencrantz (guest, #28458) [Link]

Meh. You are turning my own arguments against me. That's cheating! ;-)

Uh printf is clear?

Posted May 28, 2005 14:42 UTC (Sat) by renox (subscriber, #23785) [Link]

printf is powerful and allows complex output format without being too verbose, I agree.

But what printf isn't is "clear"! All the right and left reading matching the format element to the variable makes it not clear at all, especially when there are many variables or long text.

[ off topic rant: I wish that script language would do something like:
print "%8d{$my_variable} %-5f{$my_other_variable}\n", it would be so much easier to read instead of normal printf ]

While replacing `` by () is a good idea as ` are a pain to find in non-QWERTY keyboards, removing the distinction between "" and '' is not good, I agree with liljencrantz here.


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds