Uterine infections during pregnancy predispose to pre-term birth and postnatal morbidity, but it is unknown how prenatal bacterial exposure affects maturation of the immature gut. We hypothesised that a prenatal exposure to gram-negative lipopolysaccharide (LPS) has immunomodulatory effects that improve resistance towards necrotising enterocolitis (NEC) in pre-term neonates. At approximately 85 % gestation, pig fetuses were injected intramuscularly with saline or LPS (0·014 mg/kg), or intra-amniotically with LPS (0·4 mg/kg). Pigs were delivered by caesarean section 3-5 d later and fed colostrum (C) or formula (F) for 48 h. Gut indices did not differ between pigs injected intramuscularly with saline or LPS, and these groups were therefore pooled into two control groups according to diet (control-F, n 32 and control-C, n 11). Control-F pigs showed reduced villus heights, mucosal structure, gut integrity, digestive enzymes, elevated NEC incidence (38 v. 0 %, P < 0·05) and several differentially expressed immune-related genes, relative to control-C pigs. Compared with the control-F and control-C groups, values in formula-fed pigs given intra-amniotic LPS formula (n 17) were intermediate for villus height, enzyme activities, intestinal permeability and NEC incidence (18 %, P = 0·2 relative to control-F), and numbers of differentially expressed immune genes. In conclusion, prenatal exposure of the fetal gut to Gram-negative bacteria may modulate the immediate postnatal response to an enteral diet and colonising bacteria.