Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a bash script that needs to perform some operation on an argument which is a path The path may or may not correspond to an existing file on the user's system, so I'd like to test whether the argument looks on its face like it fits the format of a path, in a way that doesn't rely on testing for some existing file.

I hope that there is some way to do this without a regex or other pattern-matching; I'd be afraid of missing some bizarre edge case -- ideally I want to know if there is some bash builtin or something of the sort that will do this test for me.

share|improve this question
Give us example of valid path, because it is impossible to do it for all paths/cases. You can filter paths that match your criteria (never heard about builtin that could handle this). Like path depth, file extension, dir name, range etc. You can create database with list of valid file paths, otherwise you will never know, if it is correct or not - without checking. – okobaka Jan 10 '13 at 23:59

The only character that isn't allowed in a path is NUL, therefore there is no need to accommodate for natural restrictions. If you want to artificially restrict the set of valid characters (say, if you're using a non-POSIX-compliant filesystem) then you will need to check the validity of characters the hard way.

share|improve this answer
Most filesystems also impose restrictions on path lengths. – BatchyX Jan 5 '13 at 13:26
There are other rstrictions (some filesystem-bound), like total length and length of each name. The limits the standard guarantees are rather tiny, so you'd have to check for your installation. Can't find how to query the system for them right now, sorry. – vonbrand Jan 23 '13 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .